https://tour.go-zh.org/moretypes/2
https://tour.go-zh.org/list 还差两部分就学完啦哈哈,毕竟都用这个写了fab
感想:想当年C++学习的时候,都没有这么认真过,过去的时光里可真的是傻乎乎的囫囵吞枣!
//case 1
package main
import "fmt"
func main() {
i, j := 20, 100
p := &i // 指向 i
fmt.Println(*p) // 通过指针读取 i 的值
*p = 21 // 通过指针设置 i 的值
fmt.Println(i) // 查看 i 的值
p = &j // 指向 j
*p = *p / 10 // 通过指针对 j 进行除法运算
fmt.Println(j) // 查看 j 的值
}
//case 2
package main
import "fmt"
type Pos struct {
X int
Y int
Z bool
}
func main() {
t := Pos{X:1, Y:2}
p := &t
fmt.Println(t.X)
fmt.Println(p.X)
fmt.Println((*p).X)
fmt.Println((*p))
}
//case 3
package main
import "fmt"
var primes = [6]int{2, 3, 5, 7, 11, 13}
func main() {
fmt.Println(primes)
}
//case 4
package main
import "fmt"
var primes = [6]int{2, 3, 5, 7, 11, 13}
func main() {
a := primes[0:2]
b := primes[0:5]
primes[1] = 100
c := primes
c[1] = 200
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(primes)
}
//case 5
package main
import "fmt"
func printSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
func main() {
s := []int{2, 3, 5, 7, 11, 13}
printSlice(s)
// 截取切片使其长度为 0
s = s[:0]
printSlice(s)
// x x x x x x cap=6
//len=0
// 拓展其长度
s = s[:5]
printSlice(s)
//x x x x x x cap=6
//2 3 5 7 11 len=5
// 舍弃前三个值
s = s[3:]
printSlice(s)
//x x x cap=3
//7 11 len=2
// 舍弃前1个值
s = s[1:]
printSlice(s)
// x x cap=2
// 11 len=1
}
//case 9
package main
import "fmt"
func main() {
var s []int
printSlice(s)
s = append(s, 0)
printSlice(s)
//cap=1
s = append(s, 1)
printSlice(s)
//cap=2
s = append(s, 2,3,4)
printSlice(s)
//cap=6 2倍也不够,那就len+1,或者len
s = append(s, 5)
printSlice(s)
//cap=6
s = append(s, 6)
printSlice(s)
//cap=12
s = append(s, 7)
printSlice(s)
////cap=12
}
func printSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
//case 6
package main
import "fmt"
func main() {
var s []int
fmt.Println(s, len(s), cap(s))
if s == nil {
fmt.Println("nil!")
}
}
//case 8
package main
import "fmt"
func main() {
var s []int
printSlice(s)
s = append(s, 0)
printSlice(s)
//cap=1
s = append(s, 1)
printSlice(s)
//cap=2
s = append(s, 2)
printSlice(s)
//cap=4
s = append(s, 3)
printSlice(s)
//cap=4
s = append(s, 4)
printSlice(s)
//cap=8
s = append(s, 5)
printSlice(s)
//cap=8
s = append(s, 6)
printSlice(s)
//cap=8
s = append(s, 7)
printSlice(s)
//cap=8
}
func printSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
//case 10
package main
import "fmt"
func main() {
pow := make([]int, 10)
for i := range pow {
pow[i] = 1 << uint(i) // == 2**i
}
for _, value := range pow {
fmt.Printf("%d\n", value)
}
}
//case 7
package main
import "fmt"
func main() {
a := make([]int, 5)
printSlice("a", a)
// 0 0 0 0 0
b := make([]int, 0, 5)
printSlice("b", b)
// len=0 cap=5
c := b[:2]
printSlice("c", c)
printSlice("b", b)
//b cap=5 len=0
//c cap=5 len=2
d := c[2:5]
printSlice("d", d)
//d cap=3 len=3
}
func printSlice(s string, x []int) {
fmt.Printf("%s len=%d cap=%d %v\n",
s, len(x), cap(x), x)
}
//case 11
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
var a [][]uint8
for i:=0; i
//case 12
package main
import "fmt"
type Info struct {
age int
school string
}
var t map[string]Info
func main() {
t = make(map[string]Info)
t["Lucy"] = Info {
10 ,
"language school",
}
fmt.Println(t["Lucy"])
}
//case 13
package main
import (
//"golang.org/x/tour/wc"
"strings"
"fmt"
)
func WordCount(s string) map[string]int {
words := strings.Fields(s)
//
var res map[string]int
res = make(map[string]int)
for i:=0; i < len(words); i++ {
var temp string= words[i]
_, ok := res[temp]
if ok {
res[temp]++
} else {
res[temp]=1
}
}
return res
}
func WordCount2(s string) map[string]int {
words := strings.Split(s, " ")
var res map[string]int
res = make(map[string]int)
for i:=0; i < len(words); i++ {
var temp string= words[i]
_, ok := res[temp]
if ok {
res[temp]++
} else {
res[temp]=1
}
}
return res
}
func main() {
//wc.Test(WordCount)
res := WordCount("my name is Lucy")
fmt.Println(res)
res = WordCount2("my name is Lucy")
fmt.Println(res)
}
//case 14
package main
import (
"fmt"
"math"
)
func compute(fn func(float64, float64) float64) float64 {
return fn(3, 4)
}
func main() {
hypot := func(x, y float64) float64 {
return math.Sqrt(x*x + y*y)
}
fmt.Println(hypot(5, 12))
fmt.Println(compute(hypot)) // 25= 16+9
fmt.Println(compute(math.Pow)) // 3*3*3*3
}
//case 15
//pos和neg使用了两个sum副本,但是多次调用pos修改的是同一个sum
package main
import "fmt"
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
func main() {
pos, neg := adder(), adder()
for i := 0; i < 10; i++ {
fmt.Println(
pos(i),
neg(-2*i),
)
}
}
//case 16
package main
import "fmt"
// 返回一个“返回int的函数”
func fibonacci() func(int) int {
a:=0
b:=1
return func(x int) int {
if x==0 {
return a
}
if x==1 {
return b
}
c := a+b;
a = b
b = c
return b;
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f(i))
}
}