English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statements

Golang Function & Method

Golang Struct

Golang Slice & Array

Golang String (String)

Golang Pointer

Golang Interface

Golang Concurrency

Golang Exception (Error)

Golang Other Miscellaneous

Go入门教程

Introduction

GoIt is a procedural programming language. It was developed by Robert Griesemer, Rob Pike, and Ken Thompson of 2007Year development, but was 2009YearAs an open-source programming language, it is released. Programs are assembled using packages to effectively manage dependencies. The language also supports the use of patterns similar to dynamic languages in the environment. For example, type inference (y := 0 is a valid declaration for the variable y of float type).

Start from Go programming

There are various online IDEs, such as The Go Playground, repl.it, etc., which can be used to run Go programs without installation.

To install Go on your PC or laptop, we need the following two software: editor and compiler
Text editors:A text editor provides a place for you to write source codePlatform. Here is a list of text editors:

  • Windows Notepad

  • Visual Studio Code

  • Sublime

Find the Go compiler: Go distribution can beFreeBSD(version8and later versions),Linux, Mac OS X (Snow Leopard and later versions),and with32bit (386)and64bit (amd64 )of The binary installation file of the Windows operating system.)x86Processor architecture.
More information about the installation. Please visitInformation about installing GO distribution

Note: the source code file extension of the go language must be .go

Write your first program in Go:

package main  
import "fmt"
func main() {
     // Print w3codebox
     fmt.Println("Hello, ")3codebox") 
}

Output:

Hello,3codebox

Explanation of the usage of Go program syntax:

  • The1Line:It containsThe main body of the programProgramPackage, which has the overall content of the program. This is the starting point of running the program, so you must write a program.

  • The2Line:It containsimport "fmt", it is a preprocessor command that tells the compiler to include the files located in the package.

  • Third line:Main function, it is the starting point of program execution.

  • The4Line:fmt.Println()is a standard library function to print things as a screen output, this,fmtThe println method of the package has been transmitted, which is used to display output. fmt.Println() is a standard library function that is used to print certain content as output on the screen, the fmt package already includes the Println method, which is used to display output.

  • Comments:Comments are used to explain the code and are similar to those in Java or C orC ++Use it in a similar way. The compiler will ignore the comment entries and not execute them. Comments can be single-line or multi-line.

    Single-line comment:
    Syntax:

    // Single-line comment

    Multi-line comment:
    Syntax:

    /* Multi-line comment */

    Here is another example:

    package main
    import "fmt"
    func main() {
       fmt.Println("1 + 1 "=", 1 + 1)
    }

    Output:

    1 + 1 = 2