我应该将指针作为参数传递吗?

huangapple go评论84阅读模式
英文:

Should I pass pointers as parameters

问题

我正在使用指针进行工作,我有一个可能很简单的问题。

我有两个函数mainother。如果我在main函数中有一个变量,我希望在other函数中可用,我应该将其作为参数传递,还是这样做会破坏指针的目的?

选项1

func main() {
    myVar := "hello world"   
    other(&myVar)
}

func other(s *string) {
    println(s)
}

选项2

func main() {
    myVar := "hello world"
    other()   
}

func other() {
    println(*myVar) //这里能访问到myVar吗?
}
英文:

I'm working with pointers for the first time and I have what is probably a simple question.

I have two functions main and other. If I have a variable in the main function that I would like to make available in the other function should I pass it as a parameter or is that defeating the object of pointers?

OPTION 1

func main() {
    myVar := "hello world"   
    other(&myVar)
}

func other(s *string) {
    println(s)
}

OPTION 2

func main() {
    myVar := "hello world"
    other()   
}

func other() {
    println(*myVar) //Is myVar even accessible here?
}

答案1

得分: 6

不确定为什么你被投票下降了...第二个选项无法编译,因为在另一个函数内部,myVar不存在。每个变量都有一个作用域。变量只能在其作用域内访问。

(如果你想了解更多关于Go语言中不同作用域的知识,我推荐你查看以下链接https://www.golang-book.com/books/web/01-02 - 向下滚动到作用域部分。这个链接用很好的可视化方式解释了作用域。)

为了让事情更清楚,我添加了一些示例:

选项1 - 传递指针值

这是你原来的代码。但是要确保解引用指针以获取实际的字符串。你的版本只打印了指针本身(内存地址)。请看我的更改(*s而不仅仅是s)!

func main() {
    myVar := "hello world"
    other(&myVar)
}

func other(s *string) {
    println(*s)
}

选项2 - 传递变量值

这可能是你在第二个选项中想要的。

package main

func main() {
    myVar := "hello world"
    other(myVar)
}

func other(myVar string) {
    println(myVar) 
}

选项3 - 将myVar设为全局变量

也许这是你在第二个选项中想要做的。在这里,myVar是全局的(或者按照Go语言的说法,myVar具有包级别的作用域),因此可以在other函数内部访问。

var myVar = "hello world"

func main() {
    other()
}

func other() {
    println(myVar)
}

至于你是否应该传递值还是变量的指针:

通常情况下,如果你的函数需要能够编辑该值,你会传递指针。此外,当变量本身非常大,并且传递值会消耗时间/资源时,你可以传递指针,这样更有效率。

英文:

Not sure why you've got downvoted... The second option will not compile because inside the other function myVar is not present. Every variable has a scope. The variable is only accessible inside it's scope.

(If you like to learn more about the diffrent scopes in go I recommend the following link https://www.golang-book.com/books/web/01-02 - scroll down to scope. It's well explained with a nice visualization.)

To make things a bit clearer, I add some examples:

OPTION 1 - pass the pointer value

This is what you had. But make sure to dereference your pointer to get back the actual string. Your version was printing the pointer itself (mem-address). See my change (*s instead of just s)!

func main() {
	myVar := "hello world"
	other(&myVar)
}

func other(s *string) {
	println(*s)
}

OPTION 2 - pass the variable value

This is probably what you meant with your option 2.

package main

func main() {
	myVar := "hello world"
	other(myVar)
}

func other(myVar string) {
	println(myVar) 
}

OPTION 3 - make myVar global

Maybe this is what you wanted to do in your second option. myVar is global here (or as in golang lingo, myVar has a package-level scope), therefore accessible inside the other function.

var myVar = "hello world"

func main() {
	other()
}

func other() {
	println(myVar)
}

As to your question wether you should pass the value or a pointer to the variable:

In general you pass pointers, if your function needs to be able to edit the value. Also when the variable itself is really big and it would cost time / ressources to pass the value you can pass the pointer which is more efficent.

huangapple
  • 本文由 发表于 2016年1月24日 23:41:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/34977713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定