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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号