Go language code structure and grammar basis (b)

Any one language, all from the print hello world began.

 

The simplest go Code:

package main // declare main packet, the indicator is an executable program 

Import "fmt" // Import built package fmt 

FUNC main () {// main function, is the entry program executed 
    fmt .Println ( "the Hello World!") // print Hello World! in terminal 
}

To the above code, for example, when you go to write a code, you start to pay attention to is that these two:

1. The first line of each non-comment .go file must declare main package

2. All of the logic operation code must be placed in the main function performed, can not be performed outside of the main function

 

The second point above, I elaborate on the next, meaning only you can actually outside the main function defined variables , declare the function , can not execute other code. for example:

main Package 

Import "FMT" 

var S String = "Hello World" // define the variable 

const the PI = 3.14 // defines constants 

FUNC test () {// declare a function 
    FMT .Println ( "I test function") 
} 

FUNC main ( ) { 
    Test () function is executed // Test 
    FMT .Println (S, the PI) // Print 
}

Above this is no problem, but if the error will be written like this:

main Package 

Import "FMT" 

var S // String definition of variables, but no assignment 

S = "Hello World" // assignment operator (error) 

FUNC main () { 
    FMT .Println (S) 
}

Since s = "hello world" is an assignment operation can not be performed outside of the main function. So just remember: the only variables can be defined in the same time outside the main function assignment, as well as a statement function

 

Also note that the curly braces function can not start a new line, namely:

func main()
{
    fmt.Println(s)
}

This will complain.

Well, after understanding these, you can go slowly learning the grammar.

 

Why should I learn Go?

I want to say why I am studying Go. In fact, no reason, just want to learn, curious to go, as well as the status quo, emmm, helpless.

I do the front of their own learning go on my current role is nothing big. If I can choose to do the interface NodeJs, write a script tool I'll use python, learning to go I was really useless.

Today the front end of things to learn it is too much, and did not finish school, I do not know what to learn, so go might be a swap products, and let me in his spare time in contact with something else.

Of course, the front end does not need to learn it? Well, not.

 

Guess you like

Origin www.cnblogs.com/wjaaron/p/11423463.html