golang小练习
22.08.04
1 var strList = []string{} 2 in := bufio.NewScanner(os.Stdin) 3 for in.Scan() { 4 strList = append(strList, in.Text()) 5 } 6 7 var builder strings.Builder 8 for _, str := range strList { 9 builder.WriteString(str) 10 } 11 ans := builder.String() 12 fmt.Printf("%v\n", ans)
1 // 局部变量地址的变化性 2 3 func point(i int) *int { 4 ii := i 5 return &ii 6 } 7 8 func main() { 9 rand.Seed(time.Now().Unix()) 10 11 for i := 0; i < 5; i++ { 12 now := rand.Intn(100) 13 fmt.Printf("%v\n", point(now) == point(now)) 14 } 15 } 16 17 // false * 5
func t(x uint64) { for i := 0; i < 8; i++ { fmt.Println(byte(x >> (i * 8))) } // byte()就拿最后8位 fmt.Println() for i := 0; i < 8; i++ { fmt.Println(x >> (i * 8)) } } func main() { t(1024 * 1024 * 1024) } /* 0 0 0 64 0 0 0 0 1073741824 4194304 16384 64 0 0 0 0 */
func main() { // nill切片 var sli []int // empty切片 var sli2 = []int{} var sli3 = make([]int, 0) fmt.Println(sli == nil) fmt.Println(sli2 == nil) fmt.Println(sli3 == nil) } /* true false false */
实习有点忙好久没更hhh...但有在学底层理论!
2022.8.9 下班
1 func main() { 2 nwd := "NWD" 3 fmt.Printf("%q", nwd) 4 fmt.Printf("%s", nwd) 5 } 6 /* 7 "NWD"NWD 8 */
1 func main() { 2 x := 0.00 3 fmt.Println(x) 4 fmt.Println(-x) 5 fmt.Println(x / 0) 6 fmt.Println(1 / x) 7 fmt.Println(-1 / x) 8 9 fmt.Println(-1/x == math.Inf(1)) 10 fmt.Println(1/x == math.Inf(-1)) 11 fmt.Println(x/0 == math.NaN()) 12 // 浮点数中,NaN、正无穷大和负无穷大都不是唯一的 13 } 14 /* 15 0 16 -0 17 NaN 18 +Inf 19 -Inf 20 false 21 false 22 false 23 */
cmplx 复数包
https://golang.google.cn/pkg/math/cmplx/
8.10
1 func main() { 2 var arr = [...]string{1: "jan", 5: "may", 12: "dec"} 3 var arr2 = [...]string{1: "JAN", 5: "MAY", 12: "DEC"} 4 fmt.Println(arr == arr2) 5 var sli = arr[1:] 6 var sli2 = arr2[1:] 7 fmt.Println(sli == sli2) 8 } 9 // 数组可比,切片不可比较
10 // slice本身属于间接引用,由于底层数组不保证不变性,因而切片之间不可比
11 // slice == nil √合法 但建议使用len(sli) == 0进行判断
1 // sort包 诶哟不错哦 2 func main() { 3 var sli3 = []int{} 4 for i := 10; i >= 1; i-- { 5 sli3 = append(sli3, i) 6 } 7 sort.Ints(sli3) 8 fmt.Println(sli3) 9 10 var m = make(map[string]int) 11 for i := 10; i >= 1; i-- { 12 m[strconv.Itoa(i)] = i 13 } // int64转string 14 var keys = []string{} 15 fmt.Println("pre:") 16 for key, val := range m { 17 fmt.Printf("key: %v val: %v\n", key, val) 18 keys = append(keys, key) 19 } 20 sort.Strings(keys) // 记录map的key的涝顺序遍历 21 fmt.Println("after:") 22 for _, val := range keys { 23 fmt.Printf("key: %v val: %v\n", val, m[val]) 24 } 25 26 // 安全取出map中元素 27 nowVal, ok := m["nwd"] 28 if ok { 29 fmt.Printf("safe get %v", nowVal) 30 }else { 31 fmt.Printf("not found~") 32 } 33 }
1 // 将string类型slice转化为字符串 2 func trans(sli []string) string { 3 return fmt.Sprintf("%q", sli) 4 // fmt.Sprintf 返回连续长字符串 5 // %q为每项的带引号字符串 6 } 7 8 func main() { 9 var sli = []string{} 10 sli = append(sli, "nwd", "nWd", "nwD", "NWd", "NwD", "NWD") 11 fmt.Println(sli) 12 13 after := trans(sli) 14 fmt.Println(reflect.TypeOf(after)) 15 fmt.Println(after) 16 } 17 /* 18 [nwd nWd nwD NWd NwD NWD] 19 string 20 ["nwd" "nWd" "nwD" "NWd" "NwD" "NWD"] 21 */
8.11
1 type Employee struct { 2 ID int 3 Name string 4 Address string 5 DoB time.Time 6 Position string 7 Salary int 8 ManagerID int 9 } 10 11 var dilbert Employee 12 13 func EmployeeByID(id int) Employee { 14 /* ... */ 15 return dilbert 16 } 17 18 func main() { 19 dilbert.Position = "gg" 20 dilbert.ManagerID = 114514 21 22 now := EmployeeByID(dilbert.ManagerID) 23 // 下一行报错,由于函数返回的是值而不是变量,取now.ID无效 24 EmployeeByID(now.ID).Salary = 0 25 }
1 // 1. 结构体不允许自身嵌套,但允许自身的指针嵌套,可用于实现递归结构 2 type tree struct { 3 value int 4 left, right *tree 5 } 6 7 // 2. Circle嵌套于Wheel Point嵌套于Circle 8 type Point struct { 9 X int 10 Y int 11 } 12 13 type Circle struct { 14 Point Point 15 Radius float64 16 } 17 18 type Wheel struct { 19 Circle Circle 20 Spokes int 21 } 22 23 // 3. 匿名成员的直接访问 24 type BigCircle struct { 25 BigX int 26 BigY int 27 BigRadius float64 28 } 29 30 type BigWheel struct { 31 BigCircle 32 Size int 33 } 34 35 func main() { 36 t := new(tree) 37 *t = tree{value: 1, left: nil, right: nil} 38 // 上述两式等价于: 39 var tt = &tree{value: 1, left: nil, right: nil} 40 41 // 结构体的可比性 可以用作map的key?? 42 fmt.Println(t == tt) 43 44 // 结构体嵌套的链式访问 45 var wheel Wheel 46 wheel.Circle.Point.X = 1 47 wheel.Circle.Point.Y = 1 48 49 // 结构体嵌套中对子结构体匿名成员的直接访问 50 var bw BigWheel 51 bw.BigX = 10 52 bw.BigY = 10
53 54 // 字面量的赋值不支持直接访问匿名成员
55 var bww = BigWheel{BigCircle{BigX: 10, BigY: 10, BigRadius: 10}, Size: 10}
56 }
1 // struct{}可广泛用于占位符,例如map当作set使用时,充当value 2 type Set map[string]struct{} 3 4 func (set Set) has(s string) bool { 5 _, ok := set[s] 6 return ok 7 } 8 9 func (set Set) insert(s string) { 10 set[s] = struct{}{} 11 } 12 13 func (set Set) delete(s string) { 14 delete(set, s) 15 } 16 17 func main() { 18 fmt.Println(unsafe.Sizeof(struct{}{})) 19 // 0 空结构体不占用内存 20 var st struct{} 21 fmt.Println(unsafe.Sizeof(st)) 22 // 0 23 24 set := make(Set) 25 set.insert("nwd") 26 fmt.Println(set.has("nwd")) // true 27 set.delete("nwd") 28 fmt.Println(set.has("nwd")) // false 29 }