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

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

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

问题

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

  1. a := A{
  2. B: 42,
  3. C: new(int),
  4. }

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

英文:

With a type:

  1. type A struct {
  2. B int, C *int
  3. }

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

  1. a := A{
  2. B: 42,
  3. C: ?,
  4. }

答案1

得分: 15

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

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

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

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

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

  1. func makeIntPointer(v int) *int {
  2. return &v
  3. }
英文:

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:

  1. func makeIntPointer(v int) *int {
  2. return &v
  3. }

答案2

得分: 2

  1. package main
  2. import "fmt"
  3. type A struct {
  4. B int
  5. C *int
  6. }
  7. func newint(i int) *int {
  8. return &i
  9. }
  10. func main() {
  11. c := newint(5)
  12. a := &A{
  13. B: 42,
  14. C: c,
  15. }
  16. fmt.Printf(" %v", *a.C)
  17. fmt.Printf(" %#v", a.C)
  18. }
  1. package main
  2. import "fmt"
  3. type A struct {
  4. B int
  5. C *int
  6. }
  7. func newint(i int) *int {
  8. return &i
  9. }
  10. func main() {
  11. c := newint(5)
  12. a := &A{
  13. B: 42,
  14. C: c,
  15. }
  16. fmt.Printf(" %v", *a.C)
  17. fmt.Printf(" %#v", a.C)
  18. }
  1. package main
  2. import "fmt"
  3. type A struct {
  4. B int
  5. C *int
  6. }
  7. func newint(i int) *int {
  8. return &i
  9. }
  10. func main() {
  11. c := newint(5)
  12. a := &A{
  13. B: 42,
  14. C: c,
  15. }
  16. fmt.Printf(" %v", *a.C)
  17. fmt.Printf(" %#v", a.C)
  18. }
  1. package main
  2. import "fmt"
  3. type A struct {
  4. B int
  5. C *int
  6. }
  7. func newint(i int) *int {
  8. return &i
  9. }
  10. func main() {
  11. c := newint(5)
  12. a := &A{
  13. B: 42,
  14. C: c,
  15. }
  16. fmt.Printf(" %v", *a.C)
  17. fmt.Printf(" %#v", a.C)
  18. }
  1. package main
  2. import "fmt"
  3. type A struct {
  4. B int
  5. C *int
  6. }
  7. func newint(i int) *int {
  8. return &i
  9. }
  10. func main() {
  11. c := newint(5)
  12. a := &A{
  13. B: 42,
  14. C: c,
  15. }
  16. fmt.Printf(" %v", *a.C)
  17. fmt.Printf(" %#v", a.C)
  18. }
  1. package main
  2. import "fmt"
  3. type A struct {
  4. B int
  5. C *int
  6. }
  7. func newint(i int) *int {
  8. return &i
  9. }
  10. func main() {
  11. c := newint(5)
  12. a := &A{
  13. B: 42,
  14. C: c,
  15. }
  16. fmt.Printf(" %v", *a.C)
  17. fmt.Printf(" %#v", a.C)
  18. }
  1. package main
  2. import "fmt"
  3. type A struct {
  4. B int
  5. C *int
  6. }
  7. func newint(i int) *int {
  8. return &i
  9. }
  10. func main() {
  11. c := newint(5)
  12. a := &A{
  13. B: 42,
  14. C: c,
  15. }
  16. fmt.Printf(" %v", *a.C)
  17. fmt.Printf(" %#v", a.C)
  18. }
  1. package main
  2. import "fmt"
  3. type A struct {
  4. B int
  5. C *int
  6. }
  7. func newint(i int) *int {
  8. return &i
  9. }
  10. func main() {
  11. c := newint(5)
  12. a := &A{
  13. B: 42,
  14. C: c,
  15. }
  16. fmt.Printf(" %v", *a.C)
  17. fmt.Printf(" %#v", a.C)
  18. }
  1. package main
  2. import "fmt"
  3. type A struct {
  4. B int
  5. C *int
  6. }
  7. func newint(i int) *int {
  8. return &i
  9. }
  10. func main() {
  11. c := newint(5)
  12. a := &A{
  13. B: 42,
  14. C: c,
  15. }
  16. fmt.Printf(" %v", *a.C)
  17. fmt.Printf(" %#v", a.C)
  18. }
英文:
  1. package main
  2. import "fmt"
  3. type A struct {
  4. B int
  5. C *int
  6. }
  7. func newint(i int) *int{
  8. return &i
  9. }
  10. func main() {
  11. c := newint(5)
  12. a := &A{
  13. B: 42,
  14. C: c,
  15. }
  16. fmt.Printf(" %v" , *a.C)
  17. fmt.Printf(" %#v" , a.C)
  18. }

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:

确定