This is a creation in Article, where the information may have evolved or changed.
Golang declaring variables VS Ruby declaring variables
In all programming languages, I can believe that there is no more speed to live than declaring a variable in Ruby, so that when I start using Golang, I get a headache with all kinds of restrictions.
foo = 1=> 1foo.class=> Fixnum foo = 1.0=> 1.0foo.class=> Floatfoo = 1111111111111111111111111111111=> 1111111111111111111111111111111foo.class=> Bignumfoo = "bar"=> "bar"foo.class=> Stringa,b,c = 1,'2',3=> [1, "2", 3] ...
Do not need the VAR keyword, do not need to know the type, as you can see, I have told this variable, I need a 1.0, I do not need to tell him that I want a floating point, and 1.0 in Ruby is also an object, is a floating-point type of object, the real object-oriented programming language!
But in go, it might be a bit of a hassle.
The general form of declaring variables is to use var
keywords:
var identifier type
var foo int
However, go can also be used to determine the type of the variable itself
var foo = 10
And in fact, the VAR keyword can be omitted, in this case you need to =
add to the left of the number :
, but the :
left variable cannot be declared, otherwise it will cause compilation to fail
foo := 10
Multiple variables declared at the same time
var a,b,c inta,b,c = 1,2,3//或者可以省略类型,会自动判断var a,b,c = 1,2,3//var也可以省略,同样是用:a,b,c := 1,2,3