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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试