This is a created article in which the information may have evolved or changed.
Basic syntax
Package
Basic structure
The basic structure of the package is as follows
//程序运行的入口是包 `main`。package main//导入包"fmt"和"math/rand"import ("fmt""math/rand")//或import std "fmt"//指定别名import "math"// 常量的定义const PI = 3.14// 全局变量的声明与赋值var name = "gopher"// 一般类型声明type newType int// 结构的声明type gopher struct{}// 接口的声明type golang interface{}// 函数的声明func add(x int,y int) int{return x + y}// 当两个或多个连续的函数命名参数是同一类型,则除了最后一个类型之外,其他都可以省略。func add2(x, y int) int{return x + y}// 由 main 函数作为程序入口点启动func main() {fmt.Println("My favorite number is", rand.Intn(10)) fmt.Println(add(42,13)}
Alias
You can specify an alias while importing a package
import std "fmt"
When defining an alias, you can call it by using the following method
std.Println("Hello world!你好,世界!")
Note that after you define an alias, you cannot call it by its original name.
Visibility rules
Go uses casing to determine whether constants, variables, types, etc. can be called externally
By convention, the first letter of the function name is private and uppercase is public