新手使用 Go modules  GOPATH路径为包下载的路径 
golang使用go fmt(格式化代码格式)goimports(自动导包功能)   配置环境  常量  iota     开启go模块  编写go.mod模块  使用go module之后在同一个文件夹不能有相同的main函数    字符与数字可以相加,字符串和数字不能相加
a := 5.0
b := int(a)
type IT int
var a IT = 5
b := int(5)
c := IT(b)
var a int32 = 1
var b int64 = 3
b = int64(a)
fmt.Println(a, b)
 使用 _ 接受返回值,防止调用报错  使用切片是公用同一内存 切片动态扩容问题    make,new,nil  使用new和不适用new的差异  
函数:第一种和第二种定义方式
第三种定义方式:先定义返回值,return省略 
函数:第四种函数定义方式
多参数省略返回 省略号的作用:  与python相同,go语言可以将函数赋值给变量,也可以在函数中将函数当成变量传给函数,ex:
func filter(score []int, f func(int) bool) []int{
reSlice := make([]int, 0)
for _, v := range score{
if f(v){
reSlice = append(reSlice, v)
}
}
return reSlice
}
func main() {
score := []int{10, 50, 80, 90, 85}
fmt.Println(filter(score, func(i int) bool {
if i >= 60{
return true
}else {
return false
}
}))
}
关于python的finally defer语句执行时的拷贝机制,使用栈存放defer
   painc, recover  错误指的是可能出现问题的地方出现了问题,比如打开一个文件时失败,这种情况在人们的意料之中 ;而异常指的是不应该出现问题的地方出现了问题,比如引用了空指针,这种情况在人们的意料之外。可见,错误是业务过程的一部分,而异常不是 。
Golang中引入error接口类型作为错误处理的标准模式,如果函数要返回错误,则返回值类型列表中肯定包含error。error处理过程类似于C语言中的错误码,可逐层返回,直到被处理。
Golang中引入两个内置函数panic和recover来触发和终止异常处理流程,同时引入关键字defer来延迟执行defer后面的函数。
一直等到包含defer语句的函数执行完毕时,延迟函数(defer后的函数)才会被执行,而不管包含defer语句的函数是通过return的正常结束,还是由于panic导致的异常结束。你可以在一个函数中执行多条defer语句,它们的执行顺序与声明顺序相反。
当程序运行时,如果遇到引用空指针、下标越界或显式调用panic函数等情况,则先触发panic函数的执行,然后调用延迟函数。调用者继续传递panic,因此该过程一直在调用栈中重复发生:函数停止执行,调用延迟执行函数等。如果一路在延迟函数中没有recover函数的调用,则会到达该携程的起点,该携程结束,然后终止其他所有携程,包括主携程(类似于C语言中的主线程,该携程ID为1)。
错误和异常从Golang机制上讲,就是error和panic的区别。很多其他语言也一样,比如C++/Java,没有error但有errno,没有panic但有throw。
Golang错误和异常是可以互相转换的:
- 错误转异常,比如程序逻辑上尝试请求某个URL,最多尝试三次,尝试三次的过程中请求失败是错误,尝试完第三次还不成功的话,失败就被提升为异常了。
- 异常转错误,比如panic触发的异常被recover恢复后,将返回值中error类型的变量进行赋值,以便上层函数继续走错误处理流程。
      go type namedtuple  go语言的结构体:struct:   结构体的多种实例化方式  slice的大小,又结构体定义,所以占用24个字节  结构体方法
package main
import "fmt"
import "math"
type Circle struct {
x int
y int
Radius int
}
func (c Circle) Area() float64 {
return math.Pi * float64(c.Radius) * float64(c.Radius)
}
func (c Circle) Circumference() float64 {
return 2 * math.Pi * float64(c.Radius)
}
func main() {
var c = Circle {Radius: 50}
fmt.Println(c.Area(), c.Circumference())
var pc = &c
fmt.Println(pc.Area(), pc.Circumference())
}
Go 语言的方法名称也分首字母大小写,它的权限规则和字段一样,首字母大写就是公开方法,首字母小写就是内部方法,只能归属于同一个包的代码才可以访问内部方法。特性:1. 结构体的方法只能和结构体在同一个包中。2.内置的int类型不能加方法
go语言使用组合的形式实现继承
package main
import "fmt"
type Teacher struct {
name string
age int
title string
}
type Course struct {
teacher Teacher
price int
name string
url string
}
func getInfo(c Course){
fmt.Println(c.teacher.name, c.teacher.age)
}
func main() {
var c Course = Course {
teacher: Teacher{
name:"bobby",
age:18,
title: "架构师",
},
price: 100,
name: "scrapy分布式爬虫",
url: "",
}
getInfo(c)
}

package main
import "fmt"
type Teacher struct {
name string
age int
title string
}
type Course struct {
Teacher
price int
name string
url string
}
func getInfo(c Course){
fmt.Println(c.name, c.age)
}
func main() {
var c Course = Course {
Teacher: Teacher{
"bobby", 18, "",
},
price: 100,
name: "scrapy分布式爬虫",
url: "",
}
getInfo(c)
}
  
package main
import (
"fmt"
"reflect"
)
const tagName = "Testing"
type Info struct {
Name string `Testing:"-"`
Age int `Testing:"age,min=17,max=60"`
Sex string `Testing:"sex,required"`
}
func main() {
info := Info{
Name: "benben",
Age: 23,
Sex: "male",
}
t := reflect.TypeOf(info)
fmt.Println("Type:", t.Name())
fmt.Println("Kind:", t.Kind())
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
tag := field.Tag.Get(tagName)
fmt.Printf("%d. %v (%v), tag: '%v'\n", i+1, field.Name, field.Type.Name(), tag)
}
}
python的鸭子类型:一只鸟看起来像鸭子,跑起来像鸭子,叫起来像鸭子,那么这只鸟就是鸭子类型。类名不重要,重要的是这个类实现了什么方法。 
空接口的作用
第一个作用  第二个作用 第三个作用 
类型的断言
 
用接口协议去理解sort
要实现对切片的排序:编程思想:关心协议,不关心类型
package main
import (
"fmt"
"sort"
)
type Course struct {
Nmae string
Price int
Url string
}
type Courses []Course
func (c Courses) Len() int {
return len(c)
}
func (c Courses) Less(i, j int) bool{
return c[i].Price < c[j].Price
}
func (c Courses) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func main() {
courses := Courses{
Course{"django", 300, ""},
Course{"scrapy", 120, ""},
Course{"tornado", 340, ""},
}
sort.Sort(courses)
for _, v := range courses{
fmt.Println(v)
}
}
|