Ruby Programmer's Golang Tour 03

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.