go基础语法(2) | 青训营

60 阅读3分钟

面向对象

  1. OOP设计比较简洁

匿名组合

type people struct {  
name string  
}  
  
type student struct {  
people  
score int  
}  
  
func main() {  
var s1 student = student{people{"tim"}, 89}  
fmt.Printf("%+v", s1)  
}  
  1. 类似继承
  2. 基础类型也可以匿名

疑惑

什么时候会用到基础类型的匿名?

方法

type people struct {  
name string  
}  
  
type student struct {  
people  
score int  
}  
  
func (tmp student) print_info() { // 接收者:receiver  
fmt.Printf("%+v\n", tmp)  
}  
  
func (tmp *student) set_info(score int) {  
tmp.score = score  
}  
  
func main() {  
var s1 student = student{people{"tim"}, 89}  
s1.print_info()  
(&s1).set_info(80)  
s1.print_info()  
}  
  1. 类似封装
  2. 方法:带有接收者的函数
  3. 注意访问的时候:p.set_info可以内部转化为(*p).set_info(&p).set_info
  4. 两个概念:方法值和方法表达式,将成员函数赋给一个变量
  • f := student.print_info, f(s1), 方法表达式
  • f := s1.print_info, f(),方法值

接口

type humaner interface {  
sayhi()  
}  
  
type student struct {  
name string  
}  
  
func (tmp student) sayhi() {  
fmt.Println(tmp)  
}  
  
func main() {  
s1 := student{"tim"}  
s1.sayhi()  
}  
  1. 接口命名常用er结尾
  2. 其他变量或者结构体都可以实现同一个接口
  3. !!!空接口:任意类型,类似template, var template interface{} = 1, 传参的时候可以做到传入任意类型

类型断言

func main() {  
var template interface{} = 1  
if value, ok := template.(int); ok == true {  
fmt.Println(value, ok)  
}  
}  

error, panic, recover

  1. 异常处理,先跳过,以后了解!!!

文本操作

  • 字符串
  • 正则表达式
  • json

字符串

  1. 常用操作:contain, split, join, index, repeat, replace, trim, fields
  2. 转化(import "strconv"):append, parse(字符串-->其他类型), format(其他类型转为字符串)

正则表达式

  1. import "regexp"
  2. 跳过,以后了解!!!

json

  • 跨平台,跨语言,用于web服务器和客户端的信息传递
  • 在线json
  1. 生成json, 通过结构体 or map
  • 用结构体生成
import (  
"encoding/json" //import  
"fmt"  
)  
  
type Test struct {//成员变量首字母大写, ``是二次编码  
Name string `json:"name23"` //输出为name23  
Score float64 `json:"-"` //不显示  
Subject []string  
}  
  
func main() {//注意[]string, 切片  
tmp := Test{"tim", 99, []string{"chinese", "math", "english"}}  
fmt.Println(tmp)  
buf, err := json.Marshal(tmp) //转换的函数  
if err != nil {  
fmt.Println("wrong")  
return  
}  
fmt.Println(string(buf)) //string打印  
}  
  • 用Map生成
func main() {  
tmp := make(map[string]interface{}, 3)  
tmp["Name"] = "tim"  
tmp["Score"] = 99  
tmp["Subject"] = []string{"chinese", "math", "english"}  
  
buf, err := json.Marshal(tmp)  
if err != nil {  
fmt.Println("wrong")  
return  
}  
fmt.Println(string(buf))  
}  
  1. json生成结构体 or map
  • 结构体
type Test struct {  
Name string `json:"Name"` //输出为name23  
Score float64  
Subject []string  
}  
  
func main() {  
buf :=  
"{\"Name\":\"tim\",\"Score\":99,\"Subject\":[\"chinese\",\"math\",\"english\"]}"  
var tmp Test  
err := json.Unmarshal([]byte(buf), &tmp)  
if err != nil {  
fmt.Println(err)  
return  
}  
fmt.Println(tmp)  
}  
  • map同理,这里需要什么时候用结构体和map???

文件操作

  1. 先跳过,以后学!!!

并发编程

  • 协程
  • channel
  • timer
  • select

并行和并发

并行:多核多个CPU执行多个任务
并发:一个核心通过分配时间来达到执行多个任务的效果
!!!go的优势是什么???

  1. goroutine, 协程,比线程更小
func newTask() {  
for {  
fmt.Println("new Task")  
time.Sleep(time.Second)  
}  
}  
  
func main() {  
go newTask() //协程  
for {  
fmt.Println("in main")  
time.Sleep(time.Second)  
}  
}  
  • 主协程退出,其他协程也退出
  1. 先让其他协程进行
import (  
"fmt"  
"runtime"  
)  
func newTask() {  
for i := 0; i < 5; i++ {  
fmt.Println("new Task")  
}  
}  
  
func main() {  
go newTask() //协程  
for i := 0; i < 2; i++ {  
runtime.Gosched()  
fmt.Println("in main")  
}  
}  
  1. 终止协程:runtime.Goexit()

channel

ch := make(chan string, 0) //无缓冲  
ch <- "tim"  
str := <- ch  
  1. !!!需要了解:blocked,同步,异步,关闭channel, 单向channel

timer

  1. !!!也是channel相关

select

  1. !!! 监听channel

网络编程