Why don't changes made to a struct via a method persist?

前端 未结 3 801
半阙折子戏
半阙折子戏 2020-12-20 15:30

I\'m trying to understand why the following test code is not working as expected:

package main

import (
    \"fmt\"
    \"strings\"
)

type Test struct {
           


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-20 15:44

    Go is going to pass everything by value. That includes function parameters, return values and when iterating over a slice, map or channel.

    to add to @noj's answer if you switch your receiver to *Test type go will use a pointer automatically.

提交回复
热议问题