Go基础(待更新)
参考Go 语言教程
文章目录
- Go基础(待更新)
- 一、基本语法
- 1、格式化输出
- 2、声明并赋值
- 1)单变量赋值
- 2)多变量赋值
- 二、math工具包的使用
- 三、函数
- 1、参数传递
- 1)普通传递
- 2)指针传递
- 四、类型转换
- 五、面向对象
- 1、结构体
- 2、封装
- 3、继承
- 4、多态
- 六、集合
- 1、List
- 1)遍历list中的对象
一、基本语法
1、格式化输出
参考
- go语言中的输出语句
- Go语言 - fmt包 | 输出 | 占位符
格式化输出:printf("%v mod %v = %v",a,b,c)
package mainimport "fmt"func main(){fmt.Print("My weight on the surface of Mars is ")fmt.Print(149.0 * 0.3783)fmt.Print(" lbs, and I would be ")fmt.Print(41 * 365 / 687)fmt.Print(" years old.")fmt.Println()fmt.Println("My weight on the surface of Mars is", 149.0 * 0.3783, "lbs, and I would be", 41 * 365.2425/687, "years old.")// 格式化打印fmt.Println("------------------------------------格式化打印--------------------------------")fmt.Printf("My weight on the surface of Mars is %v lbs,", 149.0 * 0.3783)fmt.Printf(" and I would be %v years old.\n", 41 * 365 / 687)fmt.Printf("My weight on the surface of %v is %v lbs.\n", "Earth", 149.0)fmt.Println("-------------使用Printf对齐文本-----------")fmt.Printf("%-15v $%4v\n", "SpaceX", 94)fmt.Printf("%-15v $%4v\n", "Virgin Galactic", 100)}
// Print、Println函数可以传递若干个参数,参数之间可以用逗号隔开
// 参数可以是字符串、数字、数学表达式等
// 格式化打印:可以使用Printf来控制打印的输出结果,与Print和Println不同,Printf的第一个参数必须是字符串。// 这个字符串中包含了像%v这样的格式化动词,它的值由第二个参数的值所代替。// 如果指定了多个格式化动词,那么它们的值由后面的参数值按顺序进行替换。// 使用Printf对齐文本// 在格式化动词中指定宽度,就可以对齐文本。例如:%4v就是向左填充足够4个宽度// 正数:向左填充空格// 负数:向右填充空格
---
My weight on the surface of Mars is 56.3667 lbs, and I would be 21 years old.
My weight on the surface of Mars is 56.3667 lbs, and I would be 21.79758733624454 years old.
------------------------------------格式化打印--------------------------------
My weight on the surface of Mars is 56.3667 lbs, and I would be 21 years old.
My weight on the surface of Earth is 149 lbs.
-------------使用Printf对齐文本-----------
SpaceX $ 94
Virgin Galactic $ 100
2、声明并赋值
参考go语言的变量声明并赋值运算符(:=)
1)单变量赋值
eg1: 第8行声明并赋值变量a失败,因为变量a已经在第6行声明过了。
$ cat main.c
package mainimport "fmt"func main() {var a int = 100fmt.Printf("&a=%p\n", &a);a := 100 //报错fmt.Printf("&a=%p\n", &a);
}$ go build && main
# main
./main.go:8: no new variables on left side of :=
eg2: 虽然在第5行有声明了一个全局变量a,在第9行依然可以声明并赋值变量a,此时的a和全部变量a不是一个变量,打出来的地址不相同。
$ cat main.c
package mainimport "fmt"var a int = 100func main() {fmt.Printf("&a=%p\n", &a);a := 100 fmt.Printf("&a=%p\n", &a);
}$ go build && main
&a=0x4f8018
&a=0xc42000e268
2)多变量赋值
eg3: 第10行已经声明了变量a,**第12行就不在重新定义变量a,只定义了变量b;**可以看到两个a打印出来的是同一个。
$ cat main.c
package mainimport "fmt"func foo() (int, int) {return 100, 200
}func main() {var a int = 0;fmt.Printf("&a=%p\n", &a);a, b := foo()fmt.Printf("&a=%p, &b=%p\n", &a, &b);
}$ go build && main
&a=0xc42000e260
&a=0xc42000e260, &b=0xc42000e280
二、math工具包的使用
参考go语言基础 数学包 math
package mainimport ("fmt""math"
)func main() {/*math包:*/i := -100fmt.Println(math.Abs(float64(i))) //绝对值fmt.Println(math.Ceil(5.0)) //向上取整fmt.Println(math.Floor(5.8)) //向下取整fmt.Println(math.Mod(11, 3)) //取余数,同11%3fmt.Println(math.Modf(5.26)) //取整数,取小数fmt.Println(math.Pow(3, 2)) //x的y次方fmt.Println(math.Pow10(4)) // 10的n次方fmt.Println(math.Sqrt(8)) //开平方fmt.Println(math.Cbrt(8)) //开立方fmt.Println(math.Pi)
}
三、函数
参考
-
Go 语言函数
-
go语言:函数参数传递详解
1、参数传递
1)普通传递
eg1:
package main
import ("fmt"
)
func swap(a int, b int) {var temp inttemp = aa = bb = temp
}
func main() {x := 5y := 10swap(x, y)fmt.Print(x, y)
}
---
输出结果:5 10
eg2:
/* 函数返回两个数的最大值 */
func max(num1, num2 int) int {/* 声明局部变量 */var result intif (num1 > num2) {result = num1} else {result = num2}return result
}
2)指针传递
函数的变量不仅可以使用普通变量,还可以使用指针变量
package main
import ("fmt"
)
func swap(a *int, b *int) {var temp inttemp = *a*a = *b*b = temp
}
func main() {x := 5y := 10swap(&x, &y)fmt.Print(x, y)
}
---
输出结果:10 5
四、类型转换
参考Go 语言类型转换
go 不支持隐式转换类型(!!!),比如 :
package main
import "fmt"func main() { var a int64 = 3var b int32b = afmt.Printf("b 为 : %d", b)
}
此时会报错cannot use a (type int64) as type int32 in assignment
cannot use b (type int32) as type string in argument to fmt.Printf
但是如果改成 b = int32(a)
就不会报错了:
package main
import "fmt"func main() { var a int64 = 3var b int32b = int32(a)fmt.Printf("b 为 : %d", b)
}
五、面向对象
参考Go语言没有类怎么面向对象
1、结构体
https://www.runoob.com/go/go-structures.html
package mainimport "fmt"type Books struct {title stringauthor stringsubject stringbook_id int
}func main() {// 创建一个新的结构体fmt.Println(Books{"Go 语言", "www.runoob.com", "Go 语言教程", 6495407})// 也可以使用 key => value 格式fmt.Println(Books{title: "Go 语言", author: "www.runoob.com", subject: "Go 语言教程", book_id: 6495407})// 忽略的字段为 0 或 空fmt.Println(Books{title: "Go 语言", author: "www.runoob.com"})
}
2、封装
type Employee struct {Name stringSex stringAge int
}func (e *Employee) ToString() string {return "name=" + e.Name + ";sex=" + e.Sex + ";age=" + strconv.Itoa(e.Age)
}
3、继承
Go里面也没有像Java中类似extend继承的语法,Go是用了类似Java里组合的东西来让语法看起来像继承:
type TechEmployee struct {EmployeeSalaryPerMonth float32
}type SaleEmployee struct {EmployeeBaseSalary float32ExtraRate float32
}
对应的实例化和使用:
//实例化时,是传了个employee
tech := object.TechEmployee{Employee: object.Employee{Name: "lee"},SalaryPerMonth: 10000,
}
//这里看起来像拥有了Employee的name属性,可以设置和访问
tech.Name = "bruce lee"
fmt.Println(tech.Name)
4、多态
type TechEmployee struct {EmployeeSalaryPerMonth float32
}func (e *TechEmployee) CalcSalary() float32 {return e.SalaryPerMonth
}type Machine struct {}func (e *Machine) CalcSalary() float32 {return 0
}
在Java里比较直观,语法里直接写着实现xxx接口,Go相比的话,没那么直观,但更灵活,它没有指定实现哪个接口,而是如果定义了一个相同名字和返回值的方法,就认为是实现了对应拥有这个方法的接口,这里假如接口有两个方法,对应也必须要两个方法都有定义了,才认为是实现了接口。
六、集合
1、List
参考Go标准容器之List
package mainimport ("container/list""fmt"
)func main() {nums := list.New()nums.PushBack(1)nums.PushBack(2)nums.PushBack(3)for e := nums.Front(); e != nil; e = e.Next() {fmt.Println(e.Value)}
}
1)遍历list中的对象
参考关于go语言集合中Element转换为自定义结构体的方法
Element中Value存放的是接口,所以只要将接口转换为结构体问题就都解决了,其中conn为返回的结构体,而OK是布尔型表示有没有转换成功,true为成功。
conn , ok := (i.value).(net.Conn)
eg:
for e := testCases.Front(); e != nil; e = e.Next() {fmt.Printf("#################### testcase%v ##################\n",i)t := (e.Value).(testCase)A_w,A_j,B_w,B_j,L,bearing := t.A_w,t.A_j,t.B_w,t.B_j,t.L,t.bearing
}