Constant Parameter in golang function

前端 未结 3 649
灰色年华
灰色年华 2021-02-11 22:37

I am new to the golang. Is it possible to mark a parameter as constant in function ? So that the parameter is not modified accidentally.

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-11 23:00

    There's still a handy application to the const parameter passed by value: you can't unintentionally change the initial value.

    Consider following code:

    func Generate(count int) (value []byte) {
       value = make([]byte, count)
       for i:=0; i

    This is a valid Go code, no warning or errors during the compilation. Such kind of typo might be painful to track.

提交回复
热议问题