我应该传递指针作为参数吗? [英] Should I pass pointers as parameters
问题描述
我有两个函数 main
和其他
。如果我想在 other
函数中提供的 main
函数中有一个变量,我应该通过它作为参数还是击败指针的对象?
选项1
func main(){
myVar:=hello world
other(&myVar)
}
func其他(s *字符串){
println(s)
}
选项2
func main(){
myVar:=hello world
other()
}
func other(){
println(* myVar)//是否可以在此处访问myVar?
downvoted ...第二个选项不会编译,因为在其他函数myVar不存在。每个变量都有一个范围。
(如果您想了解更多有关不同范围的内容,我建议您使用以下链接 https://www.golang-book.com/books/web/01-02 - 为了让事情更清楚一些,我添加了一些例子:
b$ b
选项1 - 传递指针值
这就是你所拥有的。但请确保取消引用您的指针以获取实际的字符串。你的版本正在打印指针本身(mem-address)。
func main(){
myVar:=hello世界
其他(& myVar)
}
func其他(s *字符串){
println(* s)
}
选项2 - 传递变量值
这可能是您的选择2的意思。
包主
func main(){
myVar:=hello world
other(myVar)
}
func other(myVar string){
println( myVar)
}
选项3 - 使myVar成为全球
也许这就是您想要在第二个选项中执行的操作。 myVar在这里是全球性的(或者像golang lingo一样,myVar有一个包级别的作用域),因此可以在其他函数中访问。
<$ p $
$ b func main(){
> var myVar =hello world (){
println(myVar)
}
你的问题是你应该传递一个值还是一个指向变量的指针: 一般来说,如果你的函数需要编辑值。另外,当变量本身非常大,并且花费时间/资源来传递值时,您可以传递更有效的指针。 I'm working with pointers for the first time and I have what is probably a simple question. I have two functions OPTION 1 OPTION 2
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)! OPTION 2 - pass the variable value This is probably what you meant with your option 2. 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. 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. 这篇关于我应该传递指针作为参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
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?func main() {
myVar := "hello world"
other(&myVar)
}
func other(s *string) {
println(s)
}
func main() {
myVar := "hello world"
other()
}
func other() {
println(*myVar) //Is myVar even accessible here?
}
func main() {
myVar := "hello world"
other(&myVar)
}
func other(s *string) {
println(*s)
}
package main
func main() {
myVar := "hello world"
other(myVar)
}
func other(myVar string) {
println(myVar)
}
var myVar = "hello world"
func main() {
other()
}
func other() {
println(myVar)
}