码迷,mamicode.com
首页 > 编程语言 > 详细

go学习笔记-go语言传值默认采用value还是reference?

时间:2015-12-30 22:05:51      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

看下面的程序

package main
import "fmt"

type Question struct {
    title string
    detail string
}

func (ques Question) Print() {
    fmt.Println(ques.title)
}

func (ques Question) Update2(title string) {
    ques.title = title
}

func (ques *Question) Update(title string) {
    ques.title = title
}

func main() {
    ques := Question{
        title: "why",
        detail: "haha",
    }

    ques.Print()
    ques.Update("update?")
    ques.Print()
    ques.Update2("test?")
    ques.Print()
}

打印结果是:

why
update?
update?

这说明go语言默认采用的是pass by value

 

如果想达到更新对象的目的,必须手工使用指针。

go学习笔记-go语言传值默认采用value还是reference?

标签:

原文地址:http://www.cnblogs.com/inevermore/p/5090092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!