4 ms·
I'm learning Go, just a naive question, why does Go put the variable type at the end of declaration, is this an absolute need? no other widely usage language do
by ausjke 8y ago
I'm learning Go, just a naive question, why does Go put the variable type at the end of declaration, is this an absolute need? no other widely usage language does that, and it just feels odd to me.
- mmastrac 8y agoPascal did it: procedure SetColor (const NewColor: TColor; const NewFPColor: TFPColor); virtual; Rust does it too: fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 Go is similar, but less symbols: f func(func(int,int) int, int) func(int, int) int
- matt_kantor 8y agoAlso Scala, Haskell, TypeScript, Swift, Kotlin, Visual Basic, Python (PEP 526), and many others.
- amenghra 8y agoA lot of languages have had type declarations at the end. The first popular which comes to mind is Pascal (see http://wiki.freepascal.org/Fibonacci_number http://wiki.freepascal.org/Fibonacci_number). You get used to it. Having to deal with this kind of differences between different programming languages is a lesser concern in the grand scheme of things.
- ausjke 8y agoMy experience is limited to c, c++, java, all of them did 'type variable' instead of 'variable type', now I see there are many others that are doing things very differently. Thanks. Love Go's one binary does http-server-login-everything-etc, can't be simpler for deployments.
- AsyncAwait 8y ago> no other widely usage language does that Swift, Kotlin, TypeScript, Rust Like everything different, it can feel odd at first, but I actually prefer it now. Think of it as "Joe is a Person" is more natural than "Person named Joe".
- jayflux 8y agoI also believe it was More ergonomic if 2 variables have the same type: func(foo, bar int) bool
- klodolph 8y agoC++ is the weird one out, you might have a declaration like: int (*f)(int x); The variable type is around the variable name. Some of it is before and some of it is after. In Go it’s simpler: var f func(int x) int If I want a void function, I can just leave the return type off, I don’t need to add "void": var f func(int x) It’s easier to write a parser for this, because you know that this is a variable declaration just by looking at the very first token and finding a certain keyword. If you write a parser for C or C++ it’s much more complicated, because you have to keep track of which identifiers name types in the scope that you’re in. Generally, more modern languages like Java, C#, Go, and Rust are much easier to parse, they are often designed to be relatively straightforward to parse with e.g. an LL(1) parser, or close to it, maybe you can just use recursive descent. In C++ it's also a bit inconsistent, int f1(int x) { return x + 5; } std::function<int(int)> f2; auto f3 = [](int x) -> int { return x + 5 }; You also have to invent a placeholder for when there aren’t types: int x = 3; auto x = 3; In Go you just omit the type, and you don’t need a placeholder: var x int = 3 var x = 3 Other languages where the type comes after: Haskell, Python (PEP 484), ML, Rust, Pony, Nim, TypeScript, Swift. In fact I think the way C/C++/C#/Java do it, with the type at the beginning, is actually somewhat rare.