zoukankan      html  css  js  c++  java
  • Go语言学习记录之一(返回指针与返回值的区别)

    先来一个返回指针的测试,结果跟想象一样

    type A map[int]string
    type B struct {
    	A
    	c int
    }
    
    func main() {
    	b := B{make(A), 10}
    	NewB := func() *B {
    		return &b
    	}
    	c := NewB()
    	c.c = 100
    	c.A[1] = "3"
    	fmt.Println(b, c)
    }
    /* output
    {map[1:3] 100} &{map[1:3] 100}
    */
    

    再试试直接返回值,这下的输出和想象中不一样了

    type A map[int]string
    type B struct {
    	A
    	c int
    }
    
    func main() {
    	a := B{make(A), 10}
    	b := B{make(A), 10}
    	NewB := func() B {
    		return b
    	}
    	c := NewB()
    	c.c = 100
    	c.A[1] = "3"
    	// c[1]="3" invalid operation: c[1] (type B does not support indexing)
    	fmt.Println(a, b, c)
    }
    /* output
    {map[] 10} {map[1:3] 10} {map[1:3] 100}
    */
    

    为什么呢,再试试

    
    type A map[int]string
    type B struct {
    	A
    	c int
    }
    
    func main() {
    	a := make(A)
    	NewA := func() A {
    		return a
    	}
    	b := NewA()
    	b[1] = "10"
    	fmt.Println(a, b)
    }
    /* output
    map[1:10] map[1:10]
    */
    这样就编译不过了
    func main() {
    	a := make(A)
    	NewA := func() *A {
    		return &a
    	}
    	b := NewA()
    	b[1] = "10" //invalid operation: b[1] (type *A does not support indexing)
    	fmt.Println(a, b)
    }
    

    go 语言中map,slice都是指针类型,以值的形式返回struct的时候只是把map对应的指针拷贝了

  • 相关阅读:
    jquery的一些函数
    int ,long,long long范围
    DMZ
    C读取文件(如果你不知道文件内容SIZE)
    flot画饼图
    flot绘制折线图
    JS操作select相关方法:新增 修改 删除 选中 清空 判断存在
    JS-字符串操作
    websGetVar函数
    AJAX之XMLHTTPRequest对象
  • 原文地址:https://www.cnblogs.com/inu1255/p/4862731.html
Copyright © 2011-2022 走看看