初级会员
  • 第 18284 位会员
  • laijinhang
  • 2018-05-01 21:19:29
  • Offline
  • 21 65

最近发布的主题

    暂无

最近发布的文章

    暂无

最近分享的资源

    暂无

最近发布的项目

    暂无

最近的评论

  • 可以参考一下这篇文章 https://www.jianshu.com/p/11ded5e80cdf
  • 分别观察一下f1和f2函数的输出,f1函数里的是直接开辟了一块新的内存去存储,f2函数里的是先在原基础上上写的 f1中ia2 := ia[1:3]后cap(ia2)的长度是4,因为ia2 = append(ia2, 6,7,8,9),会超出4,所以直接分配一块内存,在新的内存上写 f2中ia2 := ia[1:3]后cap(ia2)的长度是4,因为逐个写的,第一次append(ia2, i)之后,没有超出4,第二次操作也没有,所以改动了原来的ia,第三次的时候,超出了4,就重新分配一个块cap(ia2)为8的内存上,并把值拷贝过去 ```go func f2() { ia := [...]int{1, 2, 3, 4, 5} ia2 := ia[1:3] fmt.Printf("len: %d, cap: %d\n",len(ia2), cap(ia2)) fmt.Printf("%v %v\n", unsafe.Pointer(&ia[0]), unsafe.Pointer(&ia2[0])) fmt.Printf("%v %v\n", unsafe.Pointer(&ia[1]), unsafe.Pointer(&ia2[1])) fmt.Printf("%v\n", unsafe.Pointer(&ia[2])) fmt.Printf("%v\n", unsafe.Pointer(&ia[3])) fmt.Printf("%v\n", unsafe.Pointer(&ia[4])) for i := 6; i < 10; i++ { fmt.Println("------------") ia2 = append(ia2, i) fmt.Printf("len: %d, cap: %d\n",len(ia2), cap(ia2)) fmt.Printf("%v %v\n", unsafe.Pointer(&ia), unsafe.Pointer(&ia2)) fmt.Printf("%v %v\n", unsafe.Pointer(&ia[1]), unsafe.Pointer(&ia2[1])) if len(ia2) < 3 { fmt.Printf("%v\n", unsafe.Pointer(&ia[2])) } else { fmt.Printf("%v %v\n", unsafe.Pointer(&ia[2]), unsafe.Pointer(&ia2[2])) } if len(ia2) < 4 { fmt.Printf("%v\n", unsafe.Pointer(&ia[3])) } else { fmt.Printf("%v %v\n", unsafe.Pointer(&ia[3]), unsafe.Pointer(&ia2[3])) } if len(ia2) < 5 { fmt.Printf("%v\n", unsafe.Pointer(&ia[4])) } else { fmt.Printf("%v %v\n", unsafe.Pointer(&ia[4]), unsafe.Pointer(&ia2[4])) } if len(ia2) == 6 { fmt.Printf("%v\n", unsafe.Pointer(&ia2[5])) } } fmt.Println(ia, ia2) } func f1() { ia := [...]int{1, 2, 3, 4, 5} ia2 := ia[1:3] fmt.Printf("%v %v\n", unsafe.Pointer(&ia), unsafe.Pointer(&ia2)) fmt.Printf("%v %v\n", unsafe.Pointer(&ia[0]), unsafe.Pointer(&ia2[0])) fmt.Printf("%v %v\n", unsafe.Pointer(&ia[1]), unsafe.Pointer(&ia2[1])) fmt.Printf("%v\n", unsafe.Pointer(&ia[2])) fmt.Printf("%v\n", unsafe.Pointer(&ia[3])) fmt.Printf("%v\n", unsafe.Pointer(&ia[4])) fmt.Println("------------") ia2 = append(ia2, 6,7,8,9) fmt.Printf("%v %v\n", unsafe.Pointer(&ia), unsafe.Pointer(&ia2)) fmt.Printf("%v %v\n", unsafe.Pointer(&ia[0]), unsafe.Pointer(&ia2[0])) fmt.Printf("%v %v\n", unsafe.Pointer(&ia[1]), unsafe.Pointer(&ia2[1])) fmt.Printf("%v %v\n", unsafe.Pointer(&ia[2]), unsafe.Pointer(&ia2[2])) fmt.Printf("%v %v\n", unsafe.Pointer(&ia[3]), unsafe.Pointer(&ia2[3])) fmt.Printf("%v %v\n", unsafe.Pointer(&ia[4]), unsafe.Pointer(&ia2[4])) fmt.Printf("%v\n", unsafe.Pointer(&ia2[5])) fmt.Println(ia, ia2) } ```
  • ```go func OpenFile(path string) (str string, err error) { f, err := os.Open(path) if err != nil { return "", err } str0, err := ioutil.ReadAll(f) if err != nil { return "", err } str = string(str0) return ConvertToString(str, "gbk", "utf-8"), nil } func ConvertToString(src string, srcCode string, tagCode string) string { srcCoder := mahonia.NewDecoder(srcCode) srcResult := srcCoder.ConvertString(src) tagCoder := mahonia.NewDecoder(tagCode) _, cdata, _ := tagCoder.Translate([]byte(srcResult), true) result := string(cdata) return result } ```
  • ![2021-05-23 15-14-36 的屏幕截图.png](https://static.studygolang.com/210523/9b91af3cca55f5d9e5cbdde3808ad759.png)