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.
-
3Absence of constant parameter does not block what we want to achieve , but i feel its presence provide clear intention of that function. For example strlen(const char *str) in c tells that it doesn't modify the input string.– Karthik G RCommented Mar 2, 2014 at 4:41
-
4Chisnall in Go Programming Language Phrasebook recommends always passing by value in Go except when you explicitly need to modify the argument:Compiler is smart enough to know what to do. One of the reasons Go was invented was so that programmers would not have to worry about such details. I also tried to make analogies to C++ and Delphi when I first started playing with Go - now I no longer do so (the "GoTo" guys here have been very helpful with that)– VectorCommented Mar 3, 2014 at 4:15
-
1The go FAQ also recommends using references for passing around big structs, even if they are not modified, for memory optimization reasons. This is actually the case when the const parameters would come in handy.– IoannaCommented Aug 17, 2017 at 13:03
4 Answers
No, this is currently not possible. There are several cases to distinguish:
- When passing a parameter "normally", i.e. by value, you don't have to worry about modifying it, since these parameters behave like local variables, so you can modify them inside the function, but your changes won't be visible outside the function. But, there is an exception to this rule...
- ...some Go types (e.g. pointers, slices, channels, maps) are reference types, which means changes to them will be visible outside of the function. Some details are given here.
- You can pass pointers (e.g., to structs) as parameters, in which case changes will be visible outside the function. If this is not intended, currently there is nothing you can do about it. So if you are passing pointers to avoid copying large structs, it is best to use this sparingly - remember, "Premature optimization is the root of all evil". Some hints are given in the Go FAQ here (it refers to method receivers, but it also applies to parameters).
-
Yes i am worrying about parameter changes propagating outside of a function. Commented Feb 28, 2014 at 16:05
-
@KarthikGR Can you simply pass by value instead of passing a pointer? For example,
func myFunc(arg int)
instead offunc myFunc(arg *int)
– weberc2Commented Feb 28, 2014 at 19:01 -
@weberc2 For struct types we usually pass it by reference so that the function stack is not large. Commented Mar 2, 2014 at 4:30
-
@KarthikGR Typically the structs I deal with are small, so I don't usually worry too much about stack size. Maybe that's bad practice on my part, but I've never experienced stack overflow (not that my experience is definitive, of course). Besides overflowing the stack, why be concerned about stack size? Does it degrade performance more so than allocating to heap?– weberc2Commented Mar 2, 2014 at 5:01
-
@KarthikGR - ok, so you are pasing a pointer to a struct as a parameter? Maybe you should have mentioned that in your question :) I was assuming you are passing the parameters by value.– rob74Commented Mar 2, 2014 at 9:37
No.
You can declare a constant inside a function body, but not as a parameter.
In Go, constants are created in compile time and can never change, while function parameters must change with each call at run time.
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<count; count++ {
value[i] = byte(i) // just for an example
}
return
}
This is a valid Go code, no warning or errors during the compilation. Such kind of typo might be painful to track.
-
-
This is not an example use case of const parameters. Value types are immutable, no reason to make values const. Commented Sep 1, 2017 at 12:29
-
Basically you can't protect yourself from a mistake in the next line of code by using a fancy language construct. You could remove the
counter
variable increment statement, and still have an error:for i:=0; i<count; {
Commented Sep 1, 2017 at 13:02
A constant can be defined in the scope where the function has been defined.
const varName varType
func myFunction() {
...
}
Parameters of a function cannot be constant because it have to be a variable to be changeable inside the function. If a function parameter was constant, we needed to put its value in a variable to do operations on it.