GO语言
DAY 1
Cent OS7 安装GO环境
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install -y golang
验证
go -version
Go is a tool for managing Go source code.
Usage:
go <command> [arguments]
The commands are:
bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
https://blog.csdn.net/li_xue_zhao/article/details/79818390 ZSH OH-MY-ZSH
https://blog.csdn.net/boling_cavalry/article/details/88598828 VIM-GO设置GO语法在VIM中高亮
编写hello
package main
//1.go以包作为管理单位
//2.每个文件必须先声明包
//3.程序必须有一个main包
import "fmt" //导入包后必须要使用
//起始花括号必须跟在main后,不能另起一行
//同理花括号必须跟函数名同行
func main() {
fmt.Println("Hello GO")//Println P必须为大写,且Println()会自动换行
//结束花括号可以跟在语句后
}
go build xxx.go 编译go代码,生成一个可执行文件
go run xxx.go 执行GO代码,不生成文件
数据类型
作用:告诉编译器这个变量(值)应该以多大的内存存储
类型(同一类的数据)
变量命名规则:字母,下划线,数字
不能以数字开头
名字不能是关键字(break var go等等)
区分大小写
变量:程序运行期间可以改变的量
声明 var XX(变量名) int(类型) //变量声明完后必须使用
同一个{}里,声明的变量名是唯一的
可以同时什么多个变量
package main
import "fmt"
func main() {
var a string
//var a int
a="test"
//a=10 a如果不赋值直接打印,返回0
fmt.Println(a)
//fmt.Println("a = ", a)
}
#返回结果test // a = 10
package main
import "fmt"
func main() {
var a int
a=10
fmt.Println("a = ",a)
a*=2 //重新赋值
fmt.Println("a=",a)
}
#返回$ go run var1.go
a = 10
a= 20
变量初始化:在声明变量的同时进行赋值
var b int = 10 //初始化
b = 20 //赋值
自动推导类型,必须初始化,通过初始化的值确定类型(常用写法)
c := 30 //这种写法不用声明变量,先声明b的类型,再给b赋值
fmt.Printf("c type is %T\n", c)//%T变量所属类型,Printf中才可以使用占位符,Println中不行,占位符在""中识别为纯文本
eg:Printf和Println的区别
package main
import "fmt"
func main() {
a:=10
fmt.Println("a =",a)
fmt.Printf("a = %d\n",a)
b,c:=20,30
fmt.Println("b =",b,"c =",c)
fmt.Printf("b = %d,c = %d\n",b,c)
}
#返回a = 10
a = 10
b = 20 c = 30
b = 20,c = 30 //注意各行的空格,保持返回值美观
多重赋值和匿名变量
a,b,c:=10,20,30 //多重赋值
交换a b的值
var tmp int
tmp = a
a = b
b = tmp
fmt.Printf("a = %d,b = %d\n",a,b)
go写法
i,j := 10,20
i,j = j, i
fmt.Printf("i = %d,j = %d\n",i,j)
#返回i = 20,j = 10
匿名变量
i,j = 10,20
tmp,_ = i,j // _ 匿名变量,丢弃数据不处理,配合函数返回值使用才有优势
fmt.Println("tmp =",tmp)
eg:
package main
import "fmt"
func test() (a,b,c int){
return 1, 2, 3
}
func main(){
var c,d,e int
c,d,e = test() //return 1,2,3
fmt.Printf("c = %d, d = %d, e = %d\n",c,d,e)
}
#返回c = 1, d = 2, e = 3
#搭配匿名变量,只返回d的值
func main(){
var c,d,e int
_,d,_ = test()
fmt.Printf("d = %d\n",d) //如果仍输出c = %d e = %d c,e 会设置值为0
}
#返回c = 0, d = 2, e = 0
常量的定义
运行期间不可以改变
使用const声明
func main(){
const a int = 10
const b = 10 //自动推导类型,常量不用使用:=
}
多个变量或者常量的定义
定义多个变量时的自动推导不用写:
eg:
package main
import "fmt"
func main(){
const(
i int = 20 // i = 20
j float64 = 6.28 //j = 6.28 可以自动推导类型
)
var (
a int =10 //a = 10
b float64 = 3.14 //b = 3.14 可以自动推导类型
)
fmt.Println("i =",i,"j =",j)
fmt.Printf("j type is %T\n",j)
fmt.Printf("a =%d,b =%f\n",a,b) //float占位符为%F
fmt.Printf("b type is %T\n",b)
}
#返回i = 20 j = 6.28 //Println返回不补0
j type is float64
a =10,b =3.140000 //Printf返回补0
b type is float64
有疑问加站长微信联系(非本文作者)