This is a creation in Article, where the information may have evolved or changed.
Go program mainly consists of the following parts:
Let's take a look at the simple "Hello World" code that will print the word:
Package Mainimport "FMT" Func Main () {/* This was my first sample program. */ FMT. Println ("Hello, world!")}
Let's take a look at the various parts of the program above:
The first line of the package body defines the name of the package. This is a must declare for Go program run in what package. The main package is the starting point to run the program. Each package has a path and name associated with it.
The next line of import "FMT" is the preprocessor command that tells the compiler to go to include files in the package fmt.
The next line of Func main () is the start of the program execution.
The next line of/*...*/is ignored by the compiler, and it has been added to the program to add comments. Therefore, such a line is called a comment in the program. Comments are also used//similar to Java or C + + annotations.
The next line of FMT. Println (... ) is to provide another feature that makes the message "Hello, world!" To be displayed on the screen. Here the FMT package has been exported, and the Println method is used to display the message on the screen.
Note the println method. In the Go language, a name if it starts with an uppercase letter for export. Export refers to a function or variable/common access to the corresponding package import.
To execute the GO program:
Let's look at how to save the source code file, and how to compile and run it. Here are the simple steps:
Open a text editor and add the above code
Save File as Hello.go
Open a command prompt and go to the directory to save the file.
Type go run Hello.go and enter to run your code.
If there is no error in your code, you can see the word "Hello world" on the screen.
$ go Run Hello.gohello, world!
Make sure that the go compiler contains the source file Hello.go in the path and directory and runs it.