Go从0开发(8)_配置/协程/管道

go ini 操作

conf.go

package main

import (
"fmt"
"gopkg.in/ini.v1"
"os"
"time"
)

type Note struct {
Content string
Cities []string
Name string
Age int `ini:"age"`
Male bool
Born time.Time
}

func main() {
cfg, err := ini.Load(`E:\Go\src\DarkHorse\studygo\day07\ini\conf.ini`)
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
n := new(Note)
err = cfg.Section("Note").MapTo(n)

fmt.Printf("%T %#v \n", n, n)
fmt.Println(n.Born)
}

conf.ini

[Note]
Content = Hi is a good man!
Cities = HangZhou, Boston
Name = Unknwon
Age = 21
Male = true
Born = 1993-01-01T20:17:05Z

strconv库  字符串转换

package main

import (
"fmt"
"strconv"
)

//整数可以直接转字符串
func f1() {
//d1 := 123456
d1 := int64(123456)
str := string(d1)
fmt.Printf("%T %v \n",str,str)
}

func f2() {

//字符串转整数
str := "123456"
ret1,_ := strconv.ParseInt(str,10,64) //字符 10进制 64位
fmt.Printf("%T \n",ret1)

//字符串转整数
int1,_ := strconv.Atoi(str)
fmt.Printf("%T \n",int1)

boolstr := "1"
boolstr1 ,_ := strconv.ParseBool(boolstr)
fmt.Printf("%T \n",boolstr1)

flt := "3.141526"
flt1,_ := strconv.ParseFloat(flt,32)
fmt.Printf("%T %f \n",flt1,flt1)

//整数转字符串
int2 := strconv.Itoa(132456)
fmt.Printf("%T \n",int2)
}

func main() {
f2()
}

并发操作

go  gorontine

package main

import (
"fmt"
"time"
)

//goroutine

func f1(i int) {
fmt.Println(i)
}

func f11(){
for i:=0;i<100;i++ {
go f1(i)
}
fmt.Println("小伙子")
time.Sleep(time.Second)
}

func f2() {
for i:=0;i<10;i++ {
fmt.Println("for值:",i)
go func() {
fmt.Println("闭包值:",i)
}()
}
fmt.Println("gogogo")
//for{}
}

func f3() {
for i:=0;i<10;i++ {
go func(i int) {
fmt.Println(i)
}(i)
}
fmt.Println("gogogo")
time.Sleep(time.Second)
}
func f4() {
go func() {
fmt.Println("我可以输出吗")
}()

fmt.Println("看看我")
}

func main() {
f4()
}

随机数

//随机数
func f1(){
rand.Seed(time.Now().UnixNano()) //随机数 种子
for i:=1;i<11;i++ {
r1 :=rand.Int()
r2 :=rand.Intn(10)
fmt.Println(r1,r2)
}

}

sync.WaitGroup


func f2(i int,ws *sync.WaitGroup) {
defer ws.Done()
time.Sleep(time.Second * time.Duration(rand.Intn(9)))
fmt.Println(i)
}

func f22() {
var ws sync.WaitGroup
for i:=1;i<11;i++ {
ws.Add(1)
go f2(i,&ws)
}
ws.Wait()
}

func main() {
f22()
}

cpu

package main

import (
"fmt"
"runtime"
"sync"
)

var wg sync.WaitGroup

func f1() {
defer wg.Done()
for i:=1;i<10;i++ {
fmt.Println("A:",i)
}
}
func f2() {
defer wg.Done()
for i:=1;i<10;i++ {
fmt.Println("B:",i)
}
}

func f3() {
wg.Add(2)
go f1()
go f2()
wg.Wait()
}

func main() {
runtime.GOMAXPROCS(1)
fmt.Println(runtime.NumCPU()) //cpu数量
f3()
}

goruntine 调度模型

GMP

channel-1 缓冲区设置

package main

import "fmt"

//channel 使用make初始化
// ch <- 10 发送10到ch
// x <- ch 接受查的数据 赋值到x
// close() 关闭
func f1() {
var ch chan int
fmt.Println(ch)
ch = make(chan int) //无缓冲区的
fmt.Println(ch)
}

func f2() {
ch := make(chan int,1) //有缓冲区的

//为啥 放在这里会死锁
ch <- 1000
fmt.Println("写入:",100)

go func() {
x:= <- ch
fmt.Println("输出:",x)
}()
//ch <- 1000
//fmt.Println("写入:",100)
close(ch)
for{}
}
func f3() {
ch := make(chan int,1) //有缓冲区的
ch <- 1000
x:= <-ch
fmt.Println(x)
}
func f4() {
ch := make(chan int) //无缓冲区的
ch <- 1000
x:= <-ch
fmt.Println(x)
}

func main() {
f4()
}

channel-2 通道互相传值

package main

import "fmt"


func f1(ch1 chan int) {

for i:=1;i<11;i++ {
ch1 <- i
}
close(ch1)
}

func f2(ch1,ch2 chan int) {
for {
x,ok := <-ch1;
if !ok{
break
}
ch2 <- x*x
}
close(ch2)
}
func f3() {
ch1 := make(chan int)
ch2 := make(chan int)
go f1(ch1)
go f2(ch1,ch2)
for{
x,ok := <-ch2;
if !ok{
break
}
fmt.Println(x)
}
}

func main() {
f3()
}

channel-3 单向通道

package main

import "fmt"


func f1(ch1 chan int) {

for i:=1;i<11;i++ {
ch1 <- i
}
close(ch1)
}

//参数是单向的通道
func f2(ch1 <-chan int,ch2 chan<- int) {
for {
x,ok := <-ch1;
if !ok{
break
}
ch2 <- x*x
}
close(ch2)
}
func f3() {
ch1 := make(chan int)
ch2 := make(chan int)
go f1(ch1)
go f2(ch1,ch2)
for{
x,ok := <-ch2;
if !ok{
break
}
fmt.Println(x)
}
}

func main() {
f3()
}




伍先生
  • 职业: 程序员,产品
  • 码龄: 8.7
  • 技能: PHP Go 前端
  • 微信: JwCode
  • 公众号/小程序: 渐悟分享