var (
a int //整形
b float32 //浮点
c complex64 //复数
d byte //字符
e string //字符串
f bool //布尔
)
fmt.Printf("%T %v \n %T \n %T \n %T \n %T \n %T \n %T \n",a,a,b,c,d,e,f) //通用打印格式
//运算符: +, +=, -, -=, *, *=, /, /=, %, %=, ==, !=, ++, --, >, <, >=, <=, >>, <<, >>=, <<=, &&, ||, !
//&a 指向a的地址
//*b 指向b地址所对应的值
if false {
//代码
}else if true {
//代码
}
switch { //switch后不写默认为true
case true:
//代码
case false:
//代码
}
nm := []int{18,58,23,49,65,91}
//冒泡排序
for i := 0 ;i < len(nm)-1 ;i++ {
for l := 0;l < len(nm)-1-i ;l++ {
if nm[l] > nm[l+1] {
nm[l],nm[l+1] = nm[l+1],nm[l]
}
}
}
//break:跳出循环
//continue:跳过当前循环
//for range
for k,v := range nm {
fmt.Printf("nm[%d] = %d \n",k,v)
}
var mun int
LOOP:
var ch chan bool
select { //随机执行case+
case <- ch :
fmt.Println("case1")
case <- ch:
fmt.Println("case2")
default:
fmt.Println("case阻塞执行")
mun++
if mun <= cap(nm) {
goto LOOP //无条件跳转到指定行
}
}
//arrry、slice、map的声明格式
//array: var name [len]type //定长数组,type表示类型
// var name [...]type //不定长数组
// var name [len][len]type //二维数组
// name := [len]type
//slice: var name []type
// var name []type = make([]type,len)
// name := make([]type,len)
// name := []type{
// ...
// }
//map: var name map[keyT]valueT //T == type
// name := make(map[keyT]vlaueT)
// var name = map[keyT]vlaueT{
// "..." : "..."
// }
//函数
//func 函数名(参数列表) (返回参数列表) {
// 函数体
//}
//结构体
type name1 struct {
//属性1 type1
//属性2 type2
//属性3,属性4 type3
}
//聚合关系:一个类作为另一个类的属性
type name2 struct {
//属性1 type1
//属性2 type2
//name1 type3
}
//继承关系;一个类作为另一个类的子类
type name3 struct {
name2
//属性2 type2
//属性3,属性4 type3
}
//接口
type name interface {
//方法体
}
//实现接口
func (变量名 结构体类型) 方法([参数列表]) [返回参数类别] {
//方法体
}
Go语言基础
于 2022-07-25 17:29:07 首次发布