diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..93e374c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +#idea +.idea/* diff --git a/MyGragmmar/myfmt.go b/MyGragmmar/myfmt.go new file mode 100644 index 0000000..361875f --- /dev/null +++ b/MyGragmmar/myfmt.go @@ -0,0 +1,51 @@ +package main + +import "fmt" + +func main() { + fmt.Printf("%x\n", 456) + var m *int + fmt.Printf("%s\n", m) + //a := 10 + b := 10 + m = &b + fmt.Printf("%d\n", *m) + + str := []string{"foo", "gomain"} + for i,s := range str{ + fmt.Println(i, s) + } + + // array + arr := [5] int{1:9, 4:1} + fmt.Println(arr) + + // arrayx2 + var value [][]int + arr1 := []int{1, 2, 3} + arr2 := []int{4, 5, 6} + value = append(value, arr1) + value = append(value, arr2) + fmt.Println(value[0]) + for i:=0; i<2; i++{ + for j:=0; j<3; j++{ + fmt.Print(value[i][j]) + } + } + + var ptr *int + + fmt.Printf("\nptr 的值为 : %x\n", ptr ) + + f1, f2 := 10, 20 + fmt.Println(f1, f2) + swap(&f1, &f2) + fmt.Println(f1, f2) +} + +func swap(a, b *int){ + temp := *a + fmt.Println(*a) + *a = *b + *b = temp +} diff --git a/MyGragmmar/myslice.go b/MyGragmmar/myslice.go new file mode 100644 index 0000000..66eaf7b --- /dev/null +++ b/MyGragmmar/myslice.go @@ -0,0 +1,48 @@ +package main + +import "fmt" + +/** +定义切片 +var slice1 []type = make([]type, len) +slice1 := make([]type, len) +指定容量,capacity +make([]T, length, capacity) +*/ + +func main() { + s := []int{1, 2, 3, 4} + s1 := make([]int, 2, 3) + fmt.Println(s) + fmt.Println(len(s1), cap(s1), s1) + fmt.Println(len(s), cap(s), s) + s2 := s[:3] + fmt.Println(s2) + + + var numbers []int + printSlice(numbers) + + /* 允许追加空切片 */ + numbers = append(numbers, 0) + printSlice(numbers) + + /* 向切片添加一个元素 */ + numbers = append(numbers, 1) + printSlice(numbers) + + /* 同时添加多个元素 */ + numbers = append(numbers, 2,3,4) + printSlice(numbers) + + /* 创建切片 numbers1 是之前切片的两倍容量*/ + numbers1 := make([]int, len(numbers), (cap(numbers))*2) + + /* 拷贝 numbers 的内容到 numbers1 */ + copy(numbers1,numbers) + printSlice(numbers1) +} + +func printSlice(x []int){ + fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x) +} \ No newline at end of file diff --git a/MyGragmmar/mystruct.go b/MyGragmmar/mystruct.go new file mode 100644 index 0000000..1d2843c --- /dev/null +++ b/MyGragmmar/mystruct.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +type Books struct { + title string + author string + subject string + id int +} + +func main() { + fmt.Println(Books{"test1", "testname", "testsub", 1}) + var book1 Books + book1.id = 2 + book1.title = "test2" + book1.author = "testname2" + fmt.Println(book1) +} \ No newline at end of file