This is a creation in Article, where the information may have evolved or changed.
Go
In the first article, I will refine the basic points that are used frequently, and for those children who have just begun to get started Go
, the foundation is not solid enough to refer to Go
the basic usage of this article for quick review.
Series Finishing:
- Go Part II: Branch statements, functions
If you are interested in the Go
language itself, you can read my translation of the advantages, disadvantages and repulsive design of the go language.
Variable definition
// 默认为零值var a stringvar b intvar a, b string // 声明变量值var e string = "abc"// 变量属于同一类型,可只写一次类型var c, d int = 3, 4// 推论类型var f, g, h, i = 3, 4, true, "def"f, g, h, i := 3, 4, true, "def"// 括号写法var ( aa = 3 ss = "kkk" bb = true)
Built-in variable type
- BOOL, string
- (u) int, (U) int8, (U) int16, (U) Int32, (U) Int64, uintptr
- Byte, Rune
- float32, Float64, complex64, complex128
Complex is a plural type
// 不用1i,程序会将 i 识别为变量fmt.Printf("%.3f\n", cmplx.Exp(1i*math.Pi) + 1)
Forcing type conversions
var a, b int = 3, 4// math.Sqrt 参数和返回值都是 float64c := int(math.Sqrt(float64(a *a + b * b)))
Definition of constants
const a, b = 3, 4const ( filename = "abc.txt" a, b = 3, 4)// const 数值可以作为任意的类型使用,所以此处 a, b变量不需要手动转换为 float64c := int(mant.Sqrt(a*a + b*b))
Self-Increment enumeration type
const ( javascript = iota c python _ php)const ( b = 1 << (10 * iota) kb mb gb tb pb)