golang return object from a function

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

golang return object from a function

问题

我正在努力理解当你从Go函数中返回一个新对象时发生了什么。

你的代码如下:

func createPointerToInt() *int {
    i := new(int)
    fmt.Println(&i)
    return i
}

func main() {
    i := createPointerToInt()
    fmt.Println(&i)
}

打印出的返回值是:

0x1040a128
0x1040a120

我会期望这两个值是相同的。我不明白为什么会有8字节的差异。

而在我看来,等价的C代码如下:

int* createPointerToInt() {
    int* i = new int;
    printf("%#08x\n", i);
    return i;
}

int main() {
    int* r = createPointerToInt();
    printf("%#08x\n", r);
    return 0;
}

返回的地址是相同的:

0x8218008
0x8218008

我是否漏掉了一些非常明显的东西?任何澄清都将不胜感激!

英文:

I am struggling to understand exactly what is happening when you return a new object from a function in go.

I have this

func createPointerToInt() *int {
    i := new(int)
	fmt.Println(&i);
    return i;
}

func main() {
	i := createPointerToInt();
    fmt.Println(&i);
}

The values printed returned are

0x1040a128
0x1040a120

I would expect these two values to be the same. I do not understand why there is an 8 byte difference.

In what I see as the equivalent C code:

int* createPointerToInt() {
    int* i = new int;
    printf("%#08x\n", i);
    return i;
}

int main() {
    int* r = createPointerToInt();
    printf("%#08x\n", r);
    return 0;
}

The address returned is the same:

0x8218008
0x8218008

Am I missing something blindingly obvious here? Any clarification would be greatly appreciated!

答案1

得分: 7

你在这里打印指针的地址 fmt.Println(&i);。试试这样做:

func main() {
    i := createPointerToInt();
    fmt.Println(i); //---> 移除 & 符号
}

i 是从 createPointerToInt 返回的指针,而 &i 是你尝试打印的指针的地址。请注意,在你的 C 示例中,你打印的是正确的:

printf("%#08x\n", r);
             ^这里没有 & 符号
英文:

You are printing the address of the pointer here fmt.Println(&i);. Try this:

func main() {
    i := createPointerToInt();
    fmt.Println(i); //--> Remove the ampersand
}

i is the pointer returned from createPointerToInt - while &i is the address of the pointer you are trying to print. Note in your C sample you are printing it correctly:

printf("%#08x\n", r);
                 ^No ampersand here

答案2

得分: 2

&i更改为i。您正在打印i的地址,而您应该打印i的值。

func createPointerToInt() *int {
     i := new(int)
     fmt.Println(*i)
     return i
}

func main() {
    i := createPointerToInt()
    fmt.Println(*i)
}
英文:

Change &i to i. You are printing address of i while you should print the value of i.

func createPointerToInt() *int {
     i := new(int)
     fmt.Println(i);
     return i;
}

func main() {
    i := createPointerToInt();
    fmt.Println(i);
}

答案3

得分: 0

但是为什么在你的原始代码中,这两个指针的地址(指针指向的内存地址除外)是不同的?

func main() {
    i := createPointerToInt();
    fmt.Println(&i);
}

等同于:

func main() {
    var i *int  // 声明变量 i
    i = createPointerToInt(); // 将声明并初始化在函数内部的不同 i 的值赋给它
    fmt.Println(&i);
}

编辑:

要打印结构体的地址,你需要使用:

fmt.Printf("%p\n", &your_struct)

golang.org/pkg/fmt/

例如:

goplayground

英文:

But how come in your original code the addresses of the two pointers (not memory addresses the pointers are pointing too) are different?

func main() {
    i := createPointerToInt();
    fmt.Println(&i);
}

Is equivalent to:

func main() {
	var i *int  // declare variable i
	i = createPointerToInt(); // assign value of
                              // a different i that was 
                              // declared and initialized
                              // inside the function
	fmt.Println(&i);
}

Edit:

To print the address of a struct you need to use:

fmt.Printf("%p\n", &your_struct)

golang.org/pkg/fmt/

For example:

goplayground

huangapple
  • 本文由 发表于 2015年3月4日 17:47:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/28851190.html
匿名

发表评论

匿名网友

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

确定