Go language structure
Before we start to learn. Go
before building the basic module of the programming language, let’s take a look at Go
structure of the simplestprogram in a language.
Go Hello World instance
The basic composition of Go
language consists of the following parts:
Package declaration
Introduction packet
Function
Variable
Statement
&
Expression.Annotation
Next let’s take a look at the simple code that outputs “Hello World!”:
Example
package main
import "fmt"
func main() {
/* This is my first simple program */
fmt.Println("Hello, World!")
}
Let’s take a look at the various parts of the above program:
The first line of code
package main
package name is defined. You must indicate which package the file belongs to in the first uncommented line of the source file, such aspackage main
.package main
Represents a program that can be executed independently, eachGo
applications contain a file namedmain
donovan’s bag.Next line
import "fmt"
tellGo
compiler this program needs to use thefmt
package (function of, or other element of)fmt
, the package implements the function to format IO (input/output).Next line
func main()
is the function that the program begins to execute.main
functions must be included in every executable program, and are generally the first to execute after startup (if anyinit()
function executes the function first.Next line
/*...*/
is a comment and will be ignored when the program is executed. Single-line comments are the most common form of comments, and youcan use them anywhere with//
the single-line comment at the beginning. Multiline comments, also known as block comments, have been marked with/*
start with a*/
at the end, and cannot be used in nesting, multiline comments are generally used for the documentation description of the package or code snippets that are commented into blocks.Next line
fmt.Println(...)
you can output the string to the console andauto matically add newline characters at the end\\n
. Usefmt.Print("hello, world\n")
the same results can be obtained.Print
andPrintln
, these two functions also support the use of variables, such asfmt.Println (arr)
. If not specified, they will set the variables in the default print formatarr
output to the console.When identifiers (including constants, variables, types, function names, structure fields, and so on) begin with an uppercase letter, such as:
Group1
an object that uses this form of identifier can then be used by the code of the external package (the client program needs to import the package first), which is called an export (like in object-oriented languagespublic
identifiers are not visible outside the package if they begin with lowercase letters, but they are visible and available inside the entire package (like in object-oriented languagesprotected
).
Execution Go
program
Let’s take a look at how to write Go
code and execute it. The steps areas follows:
Open the editor as shown in
Sublime2
to add the above code to the editor.Save the above code as
hello.go
Open the command line and go to the directory where the program file is saved.
Enter a command
go run hello.go
and press enter to execute the code.If you operate correctly, you will see “Hello World!” on the screen. The output of words.
$ go run hello.go
Hello, World!
We can also use
go build
command to generate binaries:
$ go build hello.go
$ ls
hello hello.go
$ ./hello
Hello, World!
Be careful
It is important to note that {
you cannot put it on a separate line, so the following code generates an error at run time:
Example
package main
import "fmt"
func main()
{ // Error, {cannot be on a separate line
fmt.Println("Hello, World!")
}