Before we start to learn. The basic composition of Package declaration Introduction packet Function Variable Statement Annotation Next let’s take a look at the simple code that outputs “Hello World!”: Let’s take a look at the various parts of the above program: The first line of code Next line Next line Next line Next line When identifiers (including constants, variables, types, function names, structure fields, and so on) begin with an uppercase letter, such as: Let’s take a look at how to write Open the editor as shown in Save the above code as Open the command line and go to the directory where the program file is saved. Enter a command If you operate correctly, you will see “Hello World!” on the screen. The output of words. We can also use It is important to note that Go before building the basic module of the programming language, let’s take a look at Go structure of the simplestprogram in a language. 2.3.1. Go Hello World instance #
Go language consists of the following parts: & Expression.Example #
package main import "fmt" func main() { /* This is my first simple program */ fmt.Println("Hello, World!") }
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 as package main . package main Represents a program that can be executed independently, each Go applications contain a file named main donovan’s bag. import "fmt" tell Go compiler this program needs to use the fmt package (function of, or other element of) fmt , the package implements the function to format IO (input/output). 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 any init() function executes the function first. /*...*/ 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. fmt.Println(...) you can output the string to the console andauto matically add newline characters at the end \\n . Use fmt.Print("hello, world\n") the same results can be obtained. Print and Println , these two functions also support the use of variables, such as fmt.Println (arr) . If not specified, they will set the variables in the default print format arr output to the console. 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 languages public 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 languages protected ). 2.3.2. Execution
Go program # Go code and execute it. The steps areas follows: Sublime2 to add the above code to the editor. hello.go go run hello.go and press enter to execute the code.$ go run hello.go Hello, World!
go build command to generate binaries:$ go build hello.go $ ls hello hello.go $ ./hello Hello, World!
2.3.3. Be careful #
{ 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!") }