Immutable Struct in Golang

后端 未结 3 1612
别跟我提以往
别跟我提以往 2021-01-01 09:40

Is it possible to define an immutable struct in Golang? Once initialized then only read operation on struct\'s field, no modification of field values. If so, how to do that.

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 10:16

    There is no way to mark fields/variables as read only in a generic way. The only thing you could do is marking fields/variable as unexported (first letter small) and provide public getters to prevent other packages editing variables.

提交回复
热议问题