代码基本结构
// 表示该代码位于的包
package main
// 引入外部包
import "fmt"
// 声明常量、全局变量、函数等
const CON = "abc"
var v = "abc"
func fc() string {
return "abc"
}
// 程序入口函数
func main() {
fmt.Println(con, v, fc())
}
基本数据类型
- 整数
- int
- 位数基于系统位数
- int8, int16, int32, int64
- 默认值为 0
- uint
- 位数基于系统位数
- uint8, uint16, uint32, uint64
- 在声明时不指定类型,默认类型为 int
- 如
var a = 1,a 为 int
- 如
- 在声明时制定类型但不初始化,默认值为 0
- 如
var a int,a 值为 0
- 如
- int
- 浮点数
- float64
- float32
- 默认类型为 float64
- 默认值为 0
- 布尔数
- bool
- 默认值为 false
- 字符串
- string
- 实际上是 uint8 类型的数组
- 默认值为空字符串 ""
基本语法
引入外部包
使用 import 关键字
import "{package_name}"
import (
"{package_name1}"
"{package_name2}"
...
)
// 使用
{package_name}.{func_name}(arg1, arg2, ...)
// example
import (
"fmt"
"os"
)
变量声明和初始化
Go 是一门强类型语言,每个变量在声明时必须确定类型
- 使用
var关键字声明
var {name} {type}
var (
{name1} {type1}
{name2} {type2}
...
)
// example
import (
var name string
var age int
)
不初始化必须指定变量类型,且变量会自动赋值为默认值
- 使用
var关键字声明并初始化
var {name} {type} = {value}
var {name} = {value}
// example
var str = "string"
不指定变量类型,编译器会自动识别
- 使用
:=声明并初始化
{name} := {value}
// example
n := 100
str := "string"
注:不可在声明全局变量时使用
常量声明和初始化
使用 const 关键字,与 var 类似
const {name} {type}
const {name} {type} = {value}
const {name} = {value}
const (var {name} {type} = {value}
var {name} = {value}
{name1} {type1}
{name2} {type2}
...
)
// example
const PI = 3.14
数组
存放连续相同数据类型的数据结构,可操作多维数组,没初始化的元素为默认值
// 定义
var {name} [{length}]{type}
var {name} = [{length}]{type}{{ele1}, {ele2}, ...}
{name} := [{length}]{type}{{ele1}, {ele2}, ...}
// 访问
{name}[{index}]
// example
var a [5][5]int
var b = [3]int{1, 2}
c := [2]int{}
c[0] = 1
fmt.Print(a[0], b, c[1])
条件语句
// {statement}; 可省略
if {statement}; {condition} {
...
} else if {condition} {
...
} else {
...
}
// 不需要 break
switch {expression} {
case {value1}, {value2}:
...
case {value3}:
...
default:
...
}
switch {
case {condition1}, {condition2}:
...
case {condition3}:
...
default:
...
}
// example
if n := rand.Int(); n % 2 == 0 {
fmt.Println("n is even")
} else {
fmt.Println("n is odd")
}
switch rand.Int() % 2 {
case 0:
fmt.Println("even")
case 1:
fmt.Println("odd")
}
循环语句
for {statement}; {condition}; {statement} {
...
}
// 类似 C 的 while 循环
for {condition} {
...
}
// 死循环
for {
...
}
// example
sum := 0
for i: = 0; i < 10; i++ {
sum += i
}
函数
func {name}({arg1} {type1}, {arg2} {type2}, ...) ({type3}, {type4}, ...) {
...
return {value1}, {value2}
}
// 可以直接指定返回的变量名,return 可以后面不接变量名
func {name}({arg1} {type1}, {arg2} {type2}, ...) ({name2} {type3}, {name3} {type4}, ...) {
...
return
}
// example
func myfc(a int, b int) (sum int) {
sum = a + b
return
}
指针
通常用于函数,修改实参
// 定义
var {name} *{type} = &{variable}
{name} := &{variable}
// example
func division(n *int, divisor int) (reminder int) {
reminder = *n % divisor
*n /= divisor
return
}
结构体
存放连续不同数据类型的数据结构
// 定义
type {struct_name} struct {
{element1} {type1}
{element2} {type2}
...
}
{name} := {struct_name}{{value1}, {value2}, ...}
{name} := {struct_name}{{element1}: {value1}, {element2}: {value2}, ...}
// 访问
{name}.{element}
// example
type Student struct {
name string
id string
}
Lihua = Student{"Lihua", "001"}
Lihua.id = "114514"
fmt.Println(Lihua.id)
结构体函数
给结构体绑定一个函数,类似 Java、C++ 的类函数
func ({variable_name} {struct_name}) {func_name}({arg1} {type1}, {arg2} {type2} ...) ({type3}, {type4} ...) {
...
}
// 要修改结构体的内容必须使用指针
func ({variable_name} *{struct_name}) {func_name}({arg1} {type1}, {arg2} {type2} ...) ({type3}, {type4} ...) {
...
}