Go Language learning Note one (grammar article)

Source: Internet
Author: User
Tags constant definition

National Day seven days holiday, this period of time just the project is not very tight, basically every day is the day to reconstruct the project code, at night to learn. (presumably because the people there are going on vacation, slowing down the project.) These two days "rainbow" sister also come to join in the fun, is said to go directly from Macau, and then across the sea to our big Guangdong, yesterday also in Shunde made a big temper. Bitter force of our several or every day to stay in the studio moldy, because afraid of rainbow sister to take away, so the studio doors and windows locked tightly, and then the air conditioning to 20 degrees, put on the coat, haha, this is called a cool ah! )。 Recently saw an article called "Do not hang in a technology" deep feeling, so began to learn uh but with Web development has no relevance to the go language. As to what is the go language, it has what characteristics, degree Niang a lot of, so, I do not do a detailed introduction here, directly from the grammar began. Because, I am also 0 basic start contact Go language, so inevitably there will be a lot of mistakes or inappropriate places, so, we found the left a word let me correct it, after all, blogging is to learn together and progress together. OK, no more nonsense, now go straight to the point!
As for the go environment is how to build up, here also do not say, online tutorials also have a lot. However, go to the compilation environment needs FQ download, I have an installation package placed in the Baidu Cloud disk GO1.5.1.WINDOWS-AMD64 installation package Just click to download it right now. Go on the official website Golang.com seems to have been blocked, not FQ is not see, I have a Chinese version of the Golang study site, there is a need to see the Chinese version of the Golang well, the following really began to go to the Magic Kingdom!first, let's look at how the variables of the go language are declared. The C or C + + language knows that a variable declaration is made up of "variable type" + "variable identifier", for example: int A; is declaring a variable of integer type. the variable declaration of the go language differs significantly from the C and C + + languages. For a purely variable declaration, the Go language introduces the keyword VAR (which is sure to be very exciting when you learn JavaScript), because JavaScript is also a Jiangzi statement, don't get excited, look at the back will know, the two still have a good difference?
varV1int // integral type  varv2 string // string  varV3 [10]int //ArrayvarV4 []int //array slicesvarv5 struct { // struct  fint}varV6 *int //PointersvarV7 Map[string]int //Map,key is a string type and value is of type intvarV8 Func (Aint)int // function  

By the above example, we can see that the " variable type " is placed at the end. This is different from our JavaScript because JavaScript is a weak language and does not need to describe the type of variable. Combining the above example, we know that the variable declaration syntax for GO is as follows:

var "Variable name"  "Variable type"

One thing to be reminded of is that in the example above, there is no semicolon behind every statement in the go language. Go is not required to clip a semicolon at the end of each statement. If you have enough to support, you can also add, compile can also be passed. This is not the same as C or C + +, C + + does not add semicolons can not be compiled.

Another use of the VAR keyword is that you can put several variables that need to be declared together, lest the programmer need to write the Var keyword repeatedly, as follows:
varint  v2 string)

This is a bit like the "single var mode" declaration variable in JavaScript!

Now let's look at how to initialize the variables!

The var keyword can be preserved for scenarios that require initialization when declaring variables, but are no longer necessary elements, as shown below:
var int // the correct way to use 1 var // correct use of 2, the compiler can automatically deduce the type of v2 // correct use of 3, the compiler can automatically deduce the type of V3
The effect of the above three usages is exactly the same. The third use, compared to the first, requires a much lower number of characters to be entered, and is the best choice for lazy programmers and smart programmers. The Go language also introduces another symbol that is not in C and C + + (a combination of colons and equal signs: =), which is used to explicitly express the work of declaring and initializing variables at the same time. The specified type is no longer required, and the go compiler can deduce from the right value of the initialization expression which type the variable should be declared, which makes the go language look a bit like a dynamic type language, although the go language is actually a strongly typed language (static type language). Of course, the variable that appears on the left side should not have been declared, or it will cause a compilation error, such as the following:
var int I:= 2
Results in a compilation error similar to the following:
new variables on left side of: =

Now that we know the declaration and initialization of variables, let's take a look at constants. JavaScript does not have constants, but C + + has, in fact, the constant definition of go and C is similar, all through the const keyword. Take a look at the following example:

Const Pi float64 = 3.14159265358979323846//  No type floating-point constant  = 1024x768// Untyped Integer Constants  //  u = 0.0, v = 3.0, constant multi-assignment const A, b, C = 3, 4, "foo"//  a = 3, B = 4, c = "Foo ", untyped integer and string constants
The constant definition of go can qualify a constant type, but is not required. If you define a constant without specifying a type, it is the same as the literal constant, which is an untyped constant。 The right-hand value of a constant definition can also be a constant expression that is evaluated at compile time, such as
Const MASK = 1 << 3
Since the assignment of a constant is a compile-time behavior, Therefore, an rvalue cannot have any expression that requires a run time to produce a result., such as attempting to define a constant in such a way as to cause a compilation error:
Const HOME = os. GETENV ("HOME")
The reason is simple, os. GETENV () The return result is known only at run time and cannot be determined at compile time, so the right value cannot be defined as a constant. The Declaration of variables and constants is described above, and the function of go declares that all go functions (including the type member functions mentioned in object programming) start with the keyword func. A regular function definition consists of the following sections:
func function name (argument list) (return value list) {//  function Body } // A corresponding example is as follows: int , value2 float64) (Result float64, err error) {//  function Body }
From the above example, we will find that the function of go can have more than one return value. This is not the same as C + +. The example function above compute () returns two values, one called result and the other is err. Not all return values must be assigned a value. Return values that are not explicitly assigned when the function returns are set to the default values, such as result will be set to 0.0,err will be set to nil. In order to let others understand their own code, but also in order to read in a few years later to understand the code written before, the annotation is necessary for programming. The Go program's code comments are consistent with C + +, which supports the following two uses:
/* block Annotations */ // Line Comment
Next, take a look at some of the limitations of go to the position of curly braces (especially the left curly brace "{"). In C + + or JavaScript, the left curly brace can be written at the end of a line, or it can be written at the beginning of another line. However, if you use the Go language, the left curly brace "{" is placed on a different line, which will cause the go compiler to report compilation errors, which requires special attention. For example, the following code, we write the left curly brace at the beginning of the other line, will be an error
// A corresponding example is as follows: int , value2 float64) (Result float64, err error) {//  function Body }

The error content is as follows:

Syntax error:unexpected semicolon or newline before {

The correct wording should place the left curly brace of the function at the end of the first line, as follows:

// A corresponding example is as follows: int , value2 float64) (Result float64, err error) {//  function Body }
Finally, let's take a look at the data types of go, here is simply a list of Go data types, the specific use of the next time to discuss together. The following basic types are built into the go language:? Boolean type: bool.? Integral type: int8, Byte, int16, int, uint, uintptr, etc.? Floating-point types: float32, float64.    ? Plural type: complex64, complex128.? String: String.? Character type: Rune.? Fault type: Error. In addition, the go language supports the following composite types:? pointer (pointer)? Arrays (array)? Slice (slice)? Dictionary (map)? Channel (Chan)? struct (struct)? Interface (interface)

Go Language learning Note one (grammar article)

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.