最适合网络开发者的网站
Golang。初学者课程

尿素

Go 句法


Go 语法

A Go file consists of the following parts:

  • Package declaration
  • Import packages
  • Functions
  • Statements and expressions

Look at the following code, to understand it better:

例子

软件包 main
导入(“fmt”)

func main() {
  fmt.Println("Hello World!")
}
亲自尝试 »

示例解释

Line 1: In Go, every program is part of a package. We define this using the package keyword. In this example, the program belongs to the main package.

Line 2: import ("fmt") lets us import files included in the fmt package.

Line 3: A blank line. Go ignores white space. Having white spaces in code makes it more readable.

Line 4: func main() {} is a function. Any code inside its curly brackets {} will be executed.

Line 5: fmt.Println() is a function made available from the fmt package. It is used to output/print text. In our example it will output "Hello World!".

笔记: In Go, any executable code belongs to the main package.


Go Statements

fmt.Println("Hello World!") is a statement.

In Go, statements are separated by ending a line (hitting the Enter key) or by a semicolon ";".

Hitting the Enter key adds ";" to the end of the line implicitly (does not show up in the source code).

The left curly bracket { cannot come at the start of a line.

Run the following code and see what happens:

例子

软件包 main
导入(“fmt”)

func main()
{
  fmt.Println("Hello World!")
}
亲自尝试 »

Go Compact Code

You can write more compact code, like shown below (this is not recommended because it makes the code more difficult to read):

例子

package main; import ("fmt"); func main() { fmt.Println("Hello World!");}
亲自尝试 »

围棋练习

Test Yourself With Exercises

Exercise:

Insert the missing part of the code below to output "Hello World".

package main
import ("fmt")
func main() { ("Hello World!") }