sort包中提供了很多排序算法,对自定义类型进行排序时,只需要实现sort的Interface即可,包括: func Len() int {... } func Swap(i, j int) {... } func Less(i, j int) bool {... } 使用方法举例如下: package main import ( "fmt" "sort" ) type Person struct { Name string Age int } func (p Pe
综述 变量声明时未赋初值,则变量被自动赋值为该类型的零值(固定值) func new(Type) *Type new()返回一个指针,指向新分配的该类型的零值,不是空指针(nil).the value returned is a pointer to a newly allocated zero value of that type. func make(t Type, size ...IntegerType) Type make()只适用于slice.map.chan,此三种类型创建时推荐使用
cannot define new methods on non-local type int 参考资料: http://wiki.jikexueyuan.com/project/the-way-to-go/10.6.html http://stackoverflow.com/questions/28800672/how-to-add-new-methods-to-an-existing-type-in-go
切片ik通过索引访问,然后为每个map分配内存: 切片jk通过获得切片内每个元素的拷贝来分配内存,并未成功为切片内每个map分配内存,使用时赋值也就失败了 1 package main 2 3 import fmt 4 5 func main(){ 6 ik := make([]map[int]int,5) 7 for i := range ik { 8 ik[i]=make(map[int]int,1) 9 ik[i][i]=i 10 } 11 fmt.Printf("value of ik