Go内置关键字
. | . | . | . | . |
---|---|---|---|---|
break | default | func | interface | select |
case | defer | go | map | struct |
chan | else | goto | package | switch |
const | fallthrough | if | range | type |
continue | for | import | return | var |
Go注释方法
- // :单行注释
- /* */:多行注释
//当前程序的包名
package main
//导入其他的包
import "fmt"
/* 由main函数作为程序入口点的启动 */
func main() {
fmt.Println("Hello world!")
}
@H_502_108@Go程序的一般结构
- Go程序是通过 package 来组织的(与python类似)
- 只有 package 名称为 main 的包可以包含 main 函数
- 一个可执行程序 有且仅有 一个 main 包
- 通过 import 关键字来导入其它非 main 包
//导入其他的包
import "fmt"
//导入其他的包
import (
"fmt"
"math"
)
- 通过 const 关键字来进行常量的定义
const PI = 3.1415926
var a string = "abc"
- 通过 type 关键字来进行结构(struct)或接口(interface)的声明
//一般结构声明
type newType int
//结构的声明
type gopher struct{}
//接口的声明
type golang interface{}
//接口型函数的声明
type MatchFunction func(rune,rune) bool
- 通过 func 关键字来进行函数的声明
func main() { }
fmt.Println("Hello world!")
package main
import (
fff "fmt"
)
const PI = 3.1415926
var a string = "abc"
func main() {
fff.Println(PI,a)
}
//调用第三方包的参数或函数,可以看到首字母都是大写的
fmt.Println("Hello world!")