This is a creation in Article, where the information may have evolved or changed.
1. Hello Word Writing method
package mainimport "fmt" func main (){ fmt.Printf("Hello, world")}
Compilego build helloworld.go
Run./helloworld
2. Declaration method
1、普通方式var a int = 15var b bool = false或var a intvar b boola = 15b = false2、 :=会自动匹配类型,只能在函数内使用a := 15b := false3、中括号的形式:var ( x int b bool)4、平行赋值a,b := 20,16#注意 :Go 的编译器对声明却未使用的变量在报错。5、常量,只能是数字、字符串或布尔值const( // 枚举的生成方式 a = iota // a为0 b = iota // b为1,改行的 “=iota”可省略)如果需要,可以明确指定常量的类型:const ( a = 0 b string = "0")
3. String
var s string = "hello" #Go中字符串是不可变的
If you want to modify characters, you need to use the following method
s := "hello"c := []rune(s)c[0] = 'c's2 := string(c)fmt.Printf("%s\n", s2)
Multi-line string
基于分号的置入,你需要小心使用多行字符串。s := "Starting part" + "Ending part"Go 就不会在错误的地方插入分号。另一种方式是使用反引号 ` 作为原始字符串符 号:s := `Starting part Ending part`留意最后一个例子 s 现在也包含换行。不像转义字符串标识 ,原始字符串标识的值 在引号内的字符是不转义的。
4. Array
The array is defined by [n]<type>, n indicates the length of the array, and <type> identifies the type that you want to store, and array is the value type.
var arr [10]intarr[0] = 42arr[1] = 13a := [3]int{1,2,3}或a := [...]int{1,2,3}// Go会自动统计元素的个数// 多维数组a := [3][2]int { [2]int {1,2}, [2]int {3,4}, [2]int {5,6} }或a := [3][2]int { [...]int {1,2}, [...]int {3,4}, [...]int {5,6} }或简写为a := [3][2]int { {1,2}, {3,4}, {5,6} }
5, Slice
Slice as reference type
sl := make([]int, 10)sl2 := []int{1,2,3,4}
A slice is created that holds 10 elements. It is important to note that the underlying array is not different. Slice always appears in pairs with a fixed-length array. It affects the capacity and length of the slice.
// array[n:m] 从 array 创建了一个 slice,具有元素 n 到 m-1 a := [...]int {1, 2, 3, 4, 5} // 定义一个5个元素的array,序号从0到4s1 := a[2:4] // 从序号2至3创建slice,它包含元素3,4s2 := a[1:5] s3 := a[:] // 用array中所有元素创建slice,这是a[0:len(a)]的简化写法s4 := a[:4]s5 := s2[:]// 从slice s2创建slice,注意s5仍然指向array a
The function append appends a value of 0 or other X to slice S, and returns the new appended, slice with the same type as S. If s does not have enough capacity to store the appended value, append allocates a large enough, new slice to hold the original slice element and the appended value. Therefore, the returned slice may point to a different underlying array.
s0:= []int {0, 0}// 创建一个slices1:= append(s0, 2)// 追加一个元素,s1==[]int{0,0,2}s2:= append(s1, 3, 5, 7)// 追加多个元素,s2==[]int{0,0,2,3,5,7} s3 := append(s2, s0...)// 追加一个slice,s3==[]int{0,0,3,4,5,7,0,0}。注意这三个点!
The function copy copies elements from the source slice src to the target DST, and returns the number of copied elements. The source and destination may be heavy. The number of element copies is the minimum value in Len (SRC) and Len (DST).
var a=[...]int{0,1,2,3,4,5,6,7}var s = make([]int, 6)n1 := copy(s, a[0:]) // n1 == 6, s == []int{0, 1, 2, 3, 4, 5} n2 := copy(s, s[2:]) // n2 == 4, s == []int{2, 3, 4, 5, 4, 5}
6. Map
Map can be thought of as an array indexed by a string (in its simplest form). The general way to define map is: Map[<from type>]<to type>
monthdays := map[string]int { "Jan": 31, "Feb": 28, "Mar": 31, "Apr": 30, "May": 31, "Jun": 30, "Jul": 31, "Aug": 31, "Sep": 30, "Oct": 31, "Nov": 30, "Dec": 31, // 最后一个逗号是必须的 }
Note, but only when declaring a map, use the Make form: monthdays: = Make (Map[string]int)
When iterating over an array, slice, string, or map loop, range will help you, each time it is called, it will return a key and the corresponding value.
year := 0for _, days := range monthdays { //键没有使用,因此用 _, daysyear += days}fmt.Printf("Numbers of days in a year: %d\n", year)
To add elements to a map, you can do this:
monthdays["UNDECIM"] = 30← added one months monthdays["Feb"] = 29← year rewrite this element
To check if an element exists, you can use the following method:
var value intvar present boolvalue, present = monthdays["Jan"] //如果存在,present 则有值 true或者更接近 Go 的方式v, ok := monthdays["Jan"] //“逗号 ok”形式
To remove an element from the map:
delete(monthdays, "Mar") // 删除”Mar” //通常来说语句 delete(m, x) 会删除 map 中由 m[x] 建立的实例