"Go Language" "3" Go language constants and variables

Source: Internet
Author: User
Tags constant definition modifiers sublime text

The general introduction of the language will be a lot of background and features, so that the purpose is to allow learners to have a general understanding of the language, which is why some people recommend the correct method of reading is to read the first cursory, and then read it in detail, but here I want to go directly into the details, not because of personality, But because constants and variables are the most basic components of programming languages.

First, literal constants

In the past when programming, the old staff always said not to appear in the Code Devil number, what is the devil number? A number that is written directly in the code and appears only once, for example:

Func calculateprice (price float32) float32{

return price * 0.8

}

The 0.8 here is the devil number, because it only occurs once and is written directly in the code to die, the disadvantage is not clear the surface up to 0.8 of the meaning, and the coders know that 0.8 is the meaning of the discount


The Devil number here is a literal constant, and also includes the literal constants listed below

3.1415926535897932//literal constants for floating-point types

-12//Literal constants of integer type

True//Boolean literal constant of type

Literal constants for "OK"//String type


Second, constant

Similar to other languages (C, C + +, Java, and so on), the name decorated by the const keyword is a constant , for example:

Const PI FLOAT = 3.1415926535897932

Const value INT =-12

Const flag BOOL = True

Const STR string = "OK"


Readers who have used other languages see that the code above is not accustomed to, for two reasons:

(1) The habitual quantity is indicated by the capital letter.

(2) The type is placed in front of the constant name.

But it does not matter, according to this habit to write the last 30来 times feel pleasing to the eye:


Third, variable

Go uses the var keyword to modify variables, for example:

var value int//integer variable

var address string//String variable

var money [12]float//floating-point array variable

var person struct{//structure variable

Name string

Age int

Phonenum int

}

var point *int//pointer variable to type int


Four, go constant/variable characteristics

1. No private, protected, public modifiers

Readers with experience in Java development are aware of private, protected, and public modifiers, but go says sorry to you. But how does go differentiate which constants/variables are internal and which are external?

The answer is whether the first letter of the constant name/variable name is uppercase or not, and if the first letter is capitalized , it is external and the first letter is internal ;

Since the granularity of Go is for the package, the internal and external meaning of this is whether the package is visible.

Example:

Const PI FLOAT = 3.1415926535897932//floating-point constant

Const value INT =-12//Shaping constant

var money [12]float//floating-point array variable

var person struct{//structure variable

Name string

Age int

Phonenum int

}

Here the pi, person is outside the package is visible, value, money is within the package is visible


2. The type is placed after the constant name/variable name

Readers with experience in Java development are familiar with the definition of public int value = 10, which is placed in front of the variable name value, but go on the contrary, the type is put in the back, so Daniel came out to explain, This is done to avoid programmers repeating the const or var keyword. We do not dispute whether this interpretation is appropriate, but it is natural to say that it happens.

Example:

There are two variables, one is of type int, one is a string type, and we usually define it like this.

var value int

var name string

But it can also be defined in go.

VAR (

Value int

Name string

)

So write down a var keyword


3. Constant/variable definition statements do not end with semicolons

In languages such as Java or C, it is generally defined as: const str string = "OK"; But in go lazy to put the semicolon (; ) have been omitted. Just started to write go code not good habits, always append a semicolon, but a save sublime text very thoughtful to help me omit the


4. The right value of a constant definition can be a constant expression of a compile-time operation

For example:

Const algorithm = 255 >> 2

This is similar to other languages and is easy to understand, if the right value is a run-time result expression

Const PORT = os. GETENV ("Port")

Compile error will occur when the value is not determined at compile time


"PostScript words":

start to touch go, feel it is a hodgepodge, there is a C language shadow, also has the shadow of the Java language, also has the shadow of JavaScript script, but also has the mark of Pascal language, but use the feeling this conforms to people's normal thinking, the code wants how to write on how to write, No matter how it is written, it seems to fit the go syntax. Borrowing from the user's words:Go language is a seemingly simple reality is not simple, seemingly ugly but also a sophisticated language.


This article is from the "Green Guest" blog, please be sure to keep this source http://qingkechina.blog.51cto.com/5552198/1614703

"Go Language" "3" Go language constants and variables

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.