The language has reminded me of Go in several ways, one being the availability of var and let, which are like var and const in Go. However, having read some Go code this weekend where const was used within a function body, I realized that while I use let in Swift in all possible places, I have never used const in Go for anything other than package-level values.
I would like to know if many other programmers are using const in Go to convey the intent of immutability even in small local scopes, for example within loops and closures. Further, do you like the idea of declaring immutability wherever possible, or does it add unnecessary information to code where it's clear enough that a variable is read but never overwritten?
I apologize if it seemed like I was making a hyperbolic dismissal.
That was certainly not my intent. I'm just expressing my own opinion
about the value of the let declaration.
Const cannot be used to indicate immutability the way you are implying. A const (in Go) is not a "variable" than cannot change once initialized, but more like a name given to a constant expression (practically and expression whose value can be decided at compile time). In this sense its utility in the situations you are describing would be limited.
Try this version:
http://play.golang.org/p/NFCi-usA0y
If you think about it, you'll see some if the magic in Go's handling of constants.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I've been using Swift at work for several months now. The language is nice, although the iOS API is no less of a nightmare than before.The language has reminded me of Go in several ways, one being the availability of var and let, which are like var and const in Go. However, having read some Go code this weekend where const was used within a function body, I realized that while I use let in Swift in all possible places, I have never used const in Go for anything other than package-level values.
... but my question stands as to how often people are using "const" in Go in local scopes
Start here and scan down to see several examples: https://github.com/go-stack/stack/blob/master/stack.go#L98
What are good cases for using `let` just out of curiosity?