doudi1750 2016-09-28 21:54
浏览 41
已采纳

Golang结构初始化

There is a simple struct like this:

type Event struct {
    Id         int
    Name       string
}

What is the difference between these two initialization methods?

e1 := Event{Id: 1, Name: "event 1"}
e2 := &Event{Id: 2, Name: "event 2"}

Any why would I use either of these initialization methods?

  • 写回答

2条回答 默认 最新

  • duanliang9288 2016-09-29 22:28
    关注

    The first method

    e1 := Event{Id: 1, Name: "event 1"}
    

    is initializing the variable e1 as a value with type Event.

    The second

    e2 := &Event{Id: 1, Name: "event1"}
    

    is initializing e2 as a pointer to a value of type Event As you stated in the comments, the set of methods defined on a value of a given type are a subset of the set of methods defined on a pointer to a value of that type. This means that if you have a method

    func (e Event) GetName() string {
        return e.Name
    }
    

    then both e1 and e2 can call this method, but if you had another method, say:

    func (e *Event) ChangeName(s string) {
        e.Name = s
    }
    

    Then e1 is not able to use the ChangeName method, while e2 is.

    This (e1 is not able to use the ChangeName method, while e2 is) is not the case (although it may have been at the time of writing for this help), thanks to @DannyChen for bringing this up and @GilbertNwaiwu for testing and posting in the comments below.

    (To address the striked out section above: The set of methods defined on a struct type consist of the methods defined for the type and pointers to the type.

    Instead, Go now automatically dereferences the argument to a method, so that if a method receives a pointer, Go calls the method on a pointer to that struct, and if the method receives a value, Go calls the method on the value pointed to by that struct. At this point my attempt to update this answer may be missing something important in semantics so if someone would like to correct this or clarify feel free to add a comment pointing to a more comprehensive answer. Here is a bit from the go playground illustrating this issue: https://play.golang.org/p/JcD0izXZGz.

    To some extent, this change in how pointers and values work as arguments to methods defined on function affects some areas of the discourse below but I will leave the rest unedited unless someone encourages me to update it as it seems to be more or less correct within the context of general semantics of languages that pass by value vs. pointer.)

    As to the difference between pointers and values, this example is illustrative, as pointers are ordinarily used in Go to allow you to mutate the values a variable is pointing to (but there are many more reasons one might use pointers as well! Although for typical use, this is normally a solid assumption). Thus, if you defined ChangeName instead as:

    func (e Event) ChangeName(s string) {
        e.Name = s
    }
    

    This function would not be very useful if called on the value receiver, as values (not pointers) won't keep changes that are made to them if they're passed into a function. This has to do with an area of language design around how variables are assigned and passed: What's the difference between passing by reference vs. passing by value?

    You can see this on this example in the Go Playground: https://play.golang.org/p/j7yxvu3Fe6

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?