Quantcast
Channel: How to declare an immutable variable in Go? - Stack Overflow
Browsing index pages (3 articles)

How to declare an immutable variable in Go?

I was solving some exercises to get some practice in go and came across thefollowing problem.Take this function:func foo(s string) { length := len(s); // Do something just reading from lenght}Here,...

View Article


Answer by Hymns For Disco for How to declare an immutable variable in Go?

You cannot specify a variable to be immutable. Only constants have this property.Constants in Go are also compile-time constant, meaning you can't initialize it to something that depends on the...

View Article


Answer by Zombo for How to declare an immutable variable in Go?

For something with similar result, you could use a private field. Make a modulelike this:package footype Length struct { value int }func NewLength(s string) Length { return Length{ len(s), }}func (l...

View Article
Browsing index pages (3 articles)


Latest Images