如何在结构体类型中内联初始化指针成员?

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

How to initialise a pointer member in a struct type inline?

问题

使用以下方法可以在内联方式下将指针成员初始化为非零值,而无需创建临时变量:

a := A{
    B: 42,
    C: new(int),
}

在上述代码中,我们使用new(int)来创建一个新的int类型的指针,并将其分配给C成员。这样,C成员就被初始化为一个指向非零值的指针。

英文:

With a type:

type A struct {
   B int, C *int
}

How do I initialise a pointer member to a non-zero value inline, without creating temporaries?

a := A{
    B: 42,
    C: ?,
}

答案1

得分: 15

对于你给出的具体示例,如果不引入额外的语句,你的操作是有限的。

如果你想让 C 指向一个已初始化的整数变量,你需要额外的语句来定义该变量,因为你不能取一个整数字面量的地址(即 &42 会报错)。

如果你只想将 C 初始化为指向一个新的零值,那么你很幸运,可以将它设置为 new(int)

如果你处理的是一个具有初始化语法的不同类型,你也会很幸运。例如,如果 C 是一个指向结构体的指针,你可以将它初始化为 &TheStruct{...}

如果以上都不适用,并且你真的只是为了代码清晰性而初始化 *int 变量,那么一个辅助函数可能符合你的要求。例如:

func makeIntPointer(v int) *int {
    return &v
}
英文:

For the specific example you've given you are limited in what you can do without introducing additional statements.

If you want C to point at an initialised integer variable, you will need an additional statement to define that variable since you can't take the address of an integer literal (i.e. &42 would be an error).

If you just want to initialise C as a pointer to a new zero value, you are in luck though and can set it to new(int).

If you were instead dealing with a different type that had an initialiser syntax, you would also been in luck. For example, if C was a pointer to a structure, you could initialise it to &TheStruct{...}.

If none of these are appropriate, and you are really only after code clarity for initialising *int variables, a helper function might fit your requirements. For example:

func makeIntPointer(v int) *int {
	return &v
}

答案2

得分: 2

package main

import "fmt"

type A struct {
	B int
	C *int
}

func newint(i int) *int {
	return &i
}

func main() {
	c := newint(5)
	a := &A{
		B: 42,
		C: c,
	}

	fmt.Printf(" %v", *a.C)
	fmt.Printf(" %#v", a.C)
}
package main

import "fmt"

type A struct {
	B int
	C *int
}

func newint(i int) *int {
	return &i
}

func main() {
	c := newint(5)
	a := &A{
		B: 42,
		C: c,
	}

	fmt.Printf(" %v", *a.C)
	fmt.Printf(" %#v", a.C)
}
package main

import "fmt"

type A struct {
	B int
	C *int
}

func newint(i int) *int {
	return &i
}

func main() {
	c := newint(5)
	a := &A{
		B: 42,
		C: c,
	}

	fmt.Printf(" %v", *a.C)
	fmt.Printf(" %#v", a.C)
}
package main

import "fmt"

type A struct {
	B int
	C *int
}

func newint(i int) *int {
	return &i
}

func main() {
	c := newint(5)
	a := &A{
		B: 42,
		C: c,
	}

	fmt.Printf(" %v", *a.C)
	fmt.Printf(" %#v", a.C)
}
package main

import "fmt"

type A struct {
	B int
	C *int
}

func newint(i int) *int {
	return &i
}

func main() {
	c := newint(5)
	a := &A{
		B: 42,
		C: c,
	}

	fmt.Printf(" %v", *a.C)
	fmt.Printf(" %#v", a.C)
}
package main

import "fmt"

type A struct {
	B int
	C *int
}

func newint(i int) *int {
	return &i
}

func main() {
	c := newint(5)
	a := &A{
		B: 42,
		C: c,
	}

	fmt.Printf(" %v", *a.C)
	fmt.Printf(" %#v", a.C)
}
package main

import "fmt"

type A struct {
	B int
	C *int
}

func newint(i int) *int {
	return &i
}

func main() {
	c := newint(5)
	a := &A{
		B: 42,
		C: c,
	}

	fmt.Printf(" %v", *a.C)
	fmt.Printf(" %#v", a.C)
}
package main

import "fmt"

type A struct {
	B int
	C *int
}

func newint(i int) *int {
	return &i
}

func main() {
	c := newint(5)
	a := &A{
		B: 42,
		C: c,
	}

	fmt.Printf(" %v", *a.C)
	fmt.Printf(" %#v", a.C)
}
package main

import "fmt"

type A struct {
	B int
	C *int
}

func newint(i int) *int {
	return &i
}

func main() {
	c := newint(5)
	a := &A{
		B: 42,
		C: c,
	}

	fmt.Printf(" %v", *a.C)
	fmt.Printf(" %#v", a.C)
}
英文:
package main

import "fmt"

type A struct {
   	B int 
	C *int
}

func newint(i int) *int{
	return &i
}

func main() {
	c := newint(5)
	a := &A{
    		B: 42,
    		C: c,
	     }
	
	fmt.Printf(" %v" , *a.C)
	fmt.Printf(" %#v" , a.C)
}

http://play.golang.org/p/s0HIMHoMRo

huangapple
  • 本文由 发表于 2013年10月22日 11:15:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/19508358.html
匿名

发表评论

匿名网友

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

确定