dtnwm4807 2018-08-08 20:27
浏览 61

Golang结构引用其他结构的指针或无指针以及原因

I have read some of the stack overflow question related to "why pointer and why not pointer", but I could not understand much.

So, I thought to understand and learn based on my situation on golang perspective.

I have 2 struct

type Discussion struct {
    ID      string `json:"id"`
    Title   string `json:"title"`
    Content string `json:"content"`
    Owner   *User  `json:"owner"`
}

type User struct {
    ID   string `json:"id"`
    Name string `json:"name"`
}

In above Discussion struct you can see, I used *User for Owner field.

Considering situation, I do not have to change data for Owner after creating value for Discussion like below, should I use only User or it's better to use *User like below

func main() {
    u := User {
        ID: "2",
        Name: "StackOverflow",
    }
    d := Discussion{
        ID: "1",
        Title: "This is my family",
        Content: "I love my family",
        Owner: &u,
    }
}

or, Should I use like below -

type Discussion struct {
    ID      string `json:"id"`
    Title   string `json:"title"`
    Content string `json:"content"`
    Owner   User   `json:"owner"`
}

type User struct {
    ID   string `json:"id"`
    Name string `json:"name"`
}

func main() {
    u := User {
        ID: "2",
        Name: "StackOverflow",
    }
    d := Discussion{
        ID: "1",
        Title: "This is my family",
        Content: "I love my family",
        Owner: u,
    }
}

I have following question based on above 2 situation

  1. Performance - which one is better & why?
  2. Garbage Collection - which one is better & why?
  3. Any Memory effects?
  4. Any other effects!

Which one should be used on above example and why?

Please put your answer only based on above example, which method would you have choosed above and why?

Thanks

  • 写回答

1条回答 默认 最新

  • duanjizi9443 2018-08-09 02:42
    关注
    1. Performance wise, it's better to use pointers as everything with golang is pass by value. So instead of golang having to look up the values of the User struct to then pass to the Discussion struct, it only passes the pointer to it. This also applies to how attaching methods to struct works. func (u *User)Example{} will execute faster as only the pointer has to be passed in. I think performance is negligible however

    2. No Clue, I've only read one post about garbage collection issues by twitch and they said they where all resolved by golang 1.7. There are very few situations where you are worried about this.

    3. If you don't use a pointer to User, updates to the User struct won't reflect inside of the Discussion struct. See this example here. Notice even though User's ID has been updated, it's not reflected in Discussion.

    4. /shrug

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据