Go language structure


Release date:2023-09-08 Update date:2023-10-13 Editor:admin View counts:474

Label:

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:

  1. 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 as package main. package main Represents a program that can be executed independently, each Go applications contain a file named main donovan’s bag.

  2. Next line 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).

  3. 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 any init() function executes the function first.

  4. 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.

  5. Next line 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.

  6. 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 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 ).

Execution Go program

Let’s take a look at how to write Go code and execute it. The steps areas follows:

  1. Open the editor as shown in Sublime2 to add the above code to the editor.

  2. Save the above code as hello.go

  3. Open the command line and go to the directory where the program file is saved.

  4. Enter a command go run hello.go and press enter to execute the code.

  5. If you operate correctly, you will see “Hello World!” on the screen. The output of words.

$ go run hello.go
Hello, World!
  1. 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!")
}

Powered by TorCMS (https://github.com/bukun/TorCMS).