As a beginning Go programmer, I was initially attracted to the short variable declaration syntax.
It’s great for declaring variables of a simple type. What could be wrong with letting the Go compiler infer a variable type by looking at what’s being assigned to the variable, such as:
func main() {
x := 10
}
Nothing. This is trivially the same as
func main() {
var x int
x = 10
}
I didn’t have to waste my precious time by entering an explicit type declaration for “x” when the compiler already knew what it was.
But what about something like
import “os”
func main() {
file, _ := os.Open(os.Args[1])
}
Again, nothing is wrong, at least not until I have to pass “file” to another function. For example
import “os”
func main() {
file, _ := os.Open(os.Args[1])
myfunc(file)
}
func myfunc(file ???) {
}
What type should I use to declare “file” in the parameter list for myfunc()? As a new Go programmer I have to admit that I haven’t memorized all the types used in the Go standard library. So, I have to break off working on myfunc() to look up the type returned by os.Open(). This isn’t a huge deal, but it’s a distraction and can waste time. I suppose that as I gain experience with Go, I’ll have to do this less and less.
But, it doesn’t matter how experienced I am with Go when I start using user-defined types from packages that aren’t in the Go standard library. For example (from Ben Hoyt’s excellent goawk program):
import "github.com/benhoyt/goawk/parser"
func main() {
prog, err := parser.ParseProgram(fileReader.Source(), parserConfig)
myfunc(prog)
}
func myfunc(prog ???) {
}
Since I’m going to have to look up the returned types of parse.ParseProgram anyway, what’s the advantage of using a short variable declaration? They just delay the inevitable.
Short definitions detract from one of Go’s primary goals - readability. I started using Go in the first place because I wanted a strongly typed language with explicit type declarations.
As a result of all this, I’ve started to avoid short variable declarations, except when I’m initializing a variable inside an "if", "for", or "switch” statement, such as
if v := math.Pow(x, n); v < lim {
}
for i := 0; i < 10; i++ {
}
switch os := runtime.GOOS; os {
}
In these cases I have no choice if I want to declare local temporary variables since long declarations aren’t syntactically allowed.
I doubt if this note will change anybody’s mind but it’s something to think about.
Short definitions detract from one of Go’s primary goals - readability. I started using Go in the first place because I wanted a strongly typed language with explicit type declarations.
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAGrdgiVd3BMOKE6ohRbwTmC_AhSY3Zht4LxK%3DFQjqj_YocoZAg%40mail.gmail.com.
On Apr 23, 2023, at 8:28 AM, 'Axel Wagner' via golang-nuts <golan...@googlegroups.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfFtaM2KMPxRYdaFS79O0vf91RPzQBHwHa2CLJWB6r5DJQ%40mail.gmail.com.
On Apr 23, 2023, at 9:01 AM, Robert Engels <ren...@ix.netcom.com> wrote:
Personally that syntax has always bothered me with readability. It requires lots of previous knowledge in some cases. A syntax likex, var y int = blahIs more explicit that x is reused and y is declared.
On Apr 23, 2023, at 9:07 AM, Axel Wagner <axel.wa...@googlemail.com> wrote:
import “os”
func main() {
file, _ := os.Open(os.Args[1])
myfunc(file)
}
func myfunc(file ???) {
}
What type should I use to declare “file” in the parameter list for myfunc()?
On Apr 24, 2023, at 2:14 AM, a2800276 <a280...@gmail.com> wrote:
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f6c9bafb-50a9-4e40-b091-59988417ebffn%40googlegroups.com.