GO語言基礎(chǔ)
認(rèn)識(shí)并安裝GO語言開發(fā)環(huán)境
Go語言簡(jiǎn)介
Go語言是谷歌2009年發(fā)布的第二款開源編程語言
go語言專門針對(duì)多處理器系統(tǒng)應(yīng)用程序的編程進(jìn)行了優(yōu)化,使用Go編譯的程序可以媲美C或C++代碼的速度,而且更加安全,支持并行進(jìn)程
Go語言特點(diǎn)
簡(jiǎn)潔食侮、快速号涯、安全
并行、有趣锯七、開源
內(nèi)存管理链快,數(shù)組安全,編譯迅速
Go語言開發(fā)的應(yīng)用
docker
Codis(豆瓣開發(fā)的大部分使用go語言)
Glow類似于hadoop
cockroach
一些見解
Java語言的份額繼續(xù)下滑眉尸,并最終被C和Go語言超越域蜗;
C語言將長(zhǎng)居編程榜第二的位置,并有望在Go取代java前重獲語言榜第一的寶座
Go語言最終會(huì)取代java噪猾,居于編程榜之首
Go語言環(huán)境搭建
go源碼安裝
go標(biāo)準(zhǔn)包安裝
第三方工具安裝 比如GVM
-
go語言開發(fā)工具
LiteIDE
IDE
eclipese霉祸、sublime
GO語言基礎(chǔ)知識(shí)
第一個(gè)Go應(yīng)用HelloWord
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n57" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
import "fmt"
func main(){
//go語言不強(qiáng)制加分號(hào)
fmt.Print("Hello word")
}
//cmd輸入 go run hello.go輸出結(jié)果</pre>
配置
-
go語言依賴一個(gè)重要得環(huán)境變量:$GOPATH(不是安裝目錄)
使用go get github.com/astaxie/beego命令下載框架失敗,是因?yàn)槲磁渲胓opath
set GOPATH=e:/go_path:設(shè)置gopath路徑
echo %GOPATH%:查看gopath路徑
-
gopath得作用
下載第三方代碼
包管理與引用
gopath多個(gè)包調(diào)用代碼練習(xí)
Go語言常用關(guān)鍵字
-
break default func interface select case defer go map struct chan else goto package switch const fallthrough if range type continue for import return var
package main下得main()函數(shù)是go語言得入口
-
var:關(guān)鍵字是go最基本得定義變量的方式袱蜡,與C語言不同的是Go把變量類型放在變量名后面
- 例:var x int
Go語言開發(fā)工具liteide
創(chuàng)建應(yīng)用編譯
y:="hello" //簡(jiǎn)短聲明的使用丝蹭,可定義多個(gè)
-
代碼:
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n100" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
?
import "fmt"
?
func main() {
var x int
x = 1
//這種用法須在函數(shù)體內(nèi)
y, z := "go demo", 2
fmt.Printf("%d %s %d", x, y, z)
}
?</pre>
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n100" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
go語言數(shù)據(jù)類型
bool
runne
int8、int16坪蚁、int32奔穿、int64
byte
unit8、unit16敏晤、unit32巫橄、unit64
flot32、float64
complex64茵典、complex128
string
array slice
map
其它基礎(chǔ)
數(shù)組的定義
其它數(shù)據(jù)類型的使用
s:=make([]int,10,20)//slice動(dòng)態(tài)數(shù)組對(duì)象
-
map:student:=make(map[string]int)
- make用于內(nèi)建類型(map湘换、slice、channel)的內(nèi)存分配
-
流程控制語句
if語句
-
for
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n145" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">func main() {
sum := 0
x := 1
for x <= 100 {
sum += x
x++
}
fmt.Print(sum)
}</pre>
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n145" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">func main() {
-
switch
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n150" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">x:=1
switch x{
case 1:
fmt.Print("demo1")
//GO默認(rèn)添加了break
break
case 2:
//繼續(xù)需要添加
fallthrough
case 3:
fmt.Print("demo2")
break
default 3:
fmt.Print("demo3")
}</pre>
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n150" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">x:=1
-
for循環(huán)
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n155" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">x := [5]int{1, 2, 3, 4, 5}
for i, v := range x {
fmt.Println(i, v)
}
x := make(map[string]int)
x["zhangsan"] = 78
x["lisi"] = 90
x["wangwu"] = 100
for i, v := range x {
fmt.Println(i, v)
}
x := "zhangsan"
for _, v := range x {
fmt.Printf("%c\n", v)
}</pre>
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n155" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">x := [5]int{1, 2, 3, 4, 5}
-
Go語言函數(shù)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n160" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">func funcName(input1 type1,input2 type2)(output1 type1,output2 type2){
//邏輯處理代碼
//返回多個(gè)值
return value1,value2
}
package main
?
import "fmt"
?
func swap(a int, b int) (int, n int) {
return b, a
}
func main() {
a := 1
b := 2
a, b = swap(a, b)
fmt.Printf("%d %d", a, b)
}
//匿名函數(shù)
f:=func(x,y int)int{
return x+y
}
fmt.Println(f(2,3))</pre>go語言中指針的概念
-
defer關(guān)鍵詞使執(zhí)行順序倒著走,退出或資源關(guān)閉時(shí)使用
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n167" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">defer func() {
fmt.Println("After defer")
}()
fmt.Println("befor defer")
//defer語句遇到異常繼續(xù)執(zhí)行</pre>
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n167" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">defer func() {
-
Go語言結(jié)構(gòu)struct
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n172" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
import "fmt"
type Person struct {
name string
age int
}
type Student struct{
Person//匿名字段彩倚,使用和類包含類似
speciality string
}
func main() {
person := Person{"zhangsan", 25}
// person2 :=Person{age:25,name:"wangwu"}//指定字段賦值
fmt.Printf("%v", person)
}</pre>
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n172" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
GO語言面向?qū)ο?/h4>
類的定義與使用
-
簡(jiǎn)單定義使用
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n180" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
?
import "fmt"
?
type Integer struct {
value int
}
?
func (a Integer) compare(b Integer) bool {
return a.value < b.value
}
func main() {
// var a int =1
// var b int =2
a := Integer{1}
b := Integer{2}
fmt.Printf("%v", a.compare(b))
}
?</pre>
-
go中類的初始化
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n185" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">point:=new(Point)
point:=&Point{}
point:=&Point{x:100,y:100}
point:=Point{}</pre>
-
定義與使用示例
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n190" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
import "fmt"
type Point struct {
px float32
py float32
}
func (point *Point) setxy(px, py float32) {
point.px = px
point.py = py
}
func (point *Point) getxy() (float32, float32) {
return point.px, point.py
}
func main() {
point := new(Point)
point.setxy(1.24, 4.25)
px, py := point.getxy()
fmt.Print(px, py)
}</pre>
類的繼承與使用
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n194" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
?
import "fmt"
?
type Person struct {
name string
age int
}
?
func (person Person) getNameAndAge() (string, int) {
return person.name, person.age
}
?
type Student struct {
Person
speciality string
}
?
func (student Student) getSpeciality() string {
return student.speciality
}
func main() {
student := new(Student)
student.name = "zhangsan"
student.age = 26
name, age := student.getNameAndAge()
fmt.Println(name, age)
}</pre>
-
Go語言接口
-
在go語言中筹我,一個(gè)類只需要實(shí)現(xiàn)了接口要求的所有函數(shù),我們就說這個(gè)類實(shí)現(xiàn)了該接口
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n202" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
?
import "fmt"
//非侵入式接口
type Animal interface {
Fly()
Run()
}
type Animal2 interface {
Fly()
}
type Bird struct {
}
func (bird Bird) Fly() {
fmt.Println("bird is flying")
}
func (bird Bird) Run() {
fmt.Println("bird is Run")
}
func main() {
var animal Animal
var animal2 Animal2
bird := new(Bird)
animal = bird
animal2=bird
animal2.FLy()
animal2=animal//在GO中可以接口賦值
animal.Fly()
animal.Run()
}</pre>
-
在Go語言中空間口類似泛型
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n207" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">var v1=interface{}
v1=6.78
fmt.Print(v1)
//類型查詢,看接口接收的什么類型
if v, ok := v1.(float64); ok {
fmt.Println(v, ok)
}</pre>
GO語言進(jìn)階
GO語言并發(fā)編程
Go語言并發(fā)編程之協(xié)程
與傳統(tǒng)的系統(tǒng)級(jí)線程相比,協(xié)程的大優(yōu)勢(shì)在于其"輕量級(jí)",可以輕松創(chuàng)建上百萬個(gè)而不會(huì)導(dǎo)致系統(tǒng)資源衰竭,而線程和進(jìn)程通常多也不能超過1萬個(gè),這也是協(xié)程也叫輕量級(jí)線程的原因.
-
go對(duì)協(xié)程的實(shí)現(xiàn):go+函數(shù)名:啟動(dòng)一個(gè)協(xié)程執(zhí)行函數(shù)體
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n218" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
"time"
)
func test_Routine() {
fmt.Println("This is one routine!")
}
func main() {
go test_Routine()
time.Sleep(1)
}</pre>
-
Go并發(fā)編程之channel
-
Channel:Go語言在語言級(jí)別提供的goroutine間的通訊方式
-
聲明方式:var chanName chan ElementType
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n229" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
?
import (
"fmt"
"strconv"
)
?
func Add(x, y int, quit chan int) {
z := x + y
fmt.Println(z)
quit <- 1
}
func Read(ch chan int) {
value := <-ch
fmt.Println("value:" + strconv.Itoa(value))
}
func Write(ch chan int) {
// ch < -10
}
func main() {
chs := make([]chan int, 10)
for i := 0; i < 10; i++ {
chs[i] = make(chan int)
go Add(i, i, chs[i])
}
for _, v := range chs {
<-v
}
}</pre>
-
緩存channel
-
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n235" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
"time"
)
var ch chan int
func test_channel() {
// ch := make(chan int)
ch <- 1
fmt.Println("come to end goroutline 1")
}
func main() {
ch = make(chan int, 2) //值是1和值是0的適合輸出執(zhí)行順序不同
go test_channel()
time.Sleep(2 * time.Second)
fmt.Println("running end!")
<-ch
time.Sleep(time.Second)
}
</pre>
-
select
是linux很早就引入的函數(shù),用來實(shí)現(xiàn)非阻塞的一種方式
-
go語言提供語言級(jí)別支持slelect關(guān)鍵字,用于處理異步IO問題
- <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n245" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">select{
case <-chan1://如果chan1成功讀取到數(shù)據(jù),則進(jìn)行該case處理語句
case chan2 <-1://如果成功向chan2寫入數(shù)據(jù),則進(jìn)行該case處理
default: //如果上面都沒有成功,則進(jìn)入default處理流程
}
</pre>
-
select 三個(gè)代碼示例
-
示例1
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n251" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan int)
go func(ch chan int) {
ch <- 1
}(ch)
// time.Sleep(2)//加這一句執(zhí)行結(jié)果不同
select {
case <-ch:
fmt.Print("come to read ch!")
default:
fmt.Print("come to default!")
}
}
</pre>
-
示例2
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n254" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
//超時(shí)控制的經(jīng)典實(shí)現(xiàn)
import (
"fmt"
"time"
)
func main() {
ch := make(chan int)
timeout := make(chan int, 1)
go func() {
time.Sleep(time.Second)
timeout <- 1
}()
select {
case <-ch:
fmt.Println("come to read ch!")
case <-timeout:
fmt.Println("come to timeout")
}
fmt.Print("end of code!")
}
</pre>
-
示例3
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n257" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
//超時(shí)控制的經(jīng)典實(shí)現(xiàn)
import (
"fmt"
"time"
)
func main() {
ch := make(chan int)
select {
case <-ch:
fmt.Println("come to read ch!")
case <-time.After(time.Second):
fmt.Println("come to timeout")
}
fmt.Print("end of code!")
}
</pre>
-
深入Go協(xié)程編程
-
協(xié)程與線程質(zhì)的不同
協(xié)程任務(wù)的業(yè)務(wù)代碼主動(dòng)要求切換,即主動(dòng)讓出執(zhí)行權(quán)
發(fā)生了IO,導(dǎo)致執(zhí)行阻塞
-
線程與協(xié)程代碼對(duì)比(線程幾百個(gè)就卡,協(xié)程十萬個(gè)都不算多)
-
Java線程
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n273" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">public class MyThread{
public static void main(String[] args){
new Thread(new Test_thread()).start();
new Thread(new Test_thread2()).start()
}
}
class Test_thread implements Runnable{
public void run(){
for(int i=0;i<100;i++){
System.out.println("thread 1:"+i)
}
}
}
class Test_thread2 implements Runnable{
public void run(){
for(int i=100;i<200;i++){
System.out.println("thread 2:+i);
}
}
}
</pre>
-
Go協(xié)程
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n276" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
// "runtime"
"strconv"
"time"
)
func main() {
//協(xié)程1
go func() {
for i := 1; i < 100; i++ {
if i == 10 {
// runtime.Gosched() //主動(dòng)要求切換CPU
<-ch //遇到阻塞主動(dòng)讓出,否則一直執(zhí)行下去
}
fmt.Println("routine 1:" + strconv.Itoa(i))
}
}()
//協(xié)程2
go func() {
for i := 100; i < 200; i++ {
fmt.Println("routine 2:" + strconv.Itoa(i))
}
}()
time.Sleep(time.Second)
}
</pre>
GO語言JSON與MD5
Go語言的JSON
-
使用的庫(kù)
Go語言內(nèi)置的encoding/json標(biāo)準(zhǔn)庫(kù):性能不太好
github.com/pquerna/ffjson/ffjson
-
json使用代碼
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n289" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
import (
"encoding/json"
"fmt"
)
type Student struct {
Name string //外部要引用要首字母大寫
Age int
}
func main() {
//對(duì)數(shù)組類型的json編碼
x := [5]int{1, 2, 3, 4, 5}
s, err := json.Marshal(x)
if err != nil {
panic(err)
}
fmt.Println(string(s))
//對(duì)map類型進(jìn)行json編碼
m := make(map[string]float64)
m["zhangsan"] = 100.4
s2, err2 := json.Marshal(m)
if err2 != nil {
panic(err2)
}
fmt.Println(string(s2))
//對(duì)對(duì)象進(jìn)行json編碼
student := Student{"zhangsan", 26}
s3, err3 := json.Marshal(student)
if err3 != nil {
panic(s3)
}
fmt.Println(string(s3))
//對(duì)s3進(jìn)行解碼
var s4 interface{}
json.Unmarshal([]byte(s3), &s4)
fmt.Printf("%v", s4)
}
</pre>
-
md5使用
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n292" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
import (
"crypto/md5"
"fmt"
)
func main() {
Md5Inst := md5.New()
Md5Inst.Write([]byte("zhangsan"))
Result := Md5Inst.Sum([]byte(""))
fmt.Printf("%x\n\n", Result)
}
</pre>
GO語言HTTP
go語言標(biāo)準(zhǔn)庫(kù)內(nèi)建提供了net/http包
-
處理http請(qǐng)求:使用net/http包提供的http.ListenAndServer()方法,可以在指定的地址進(jìn)行監(jiān)聽,開啟一個(gè)HTTP.服務(wù)端該方法的原型如下:func ListenAndServe(addr string,hander Handler)error
- 該方法用于在指定的TCP網(wǎng)絡(luò)地址addr進(jìn)行監(jiān)聽,然后調(diào)用服務(wù)端處理程序來處理傳入的鏈接請(qǐng)求.該方法有兩個(gè)參數(shù):第一個(gè)參數(shù)addr即監(jiān)聽地址;第二個(gè)服務(wù)表示服務(wù)端處理程序,通常為空,這意味著服務(wù)端調(diào)用http.DefaultServeMux進(jìn)行處理,而服務(wù)端編寫的業(yè)務(wù)邏輯處理程序http.Handle()或http.HandleFunc()默認(rèn)注入http.DefaultServeMux中
-
處理https請(qǐng)求
func ListenAndServeTLS(addr string,certFile string,keyFile string,handler Handler) error
-
http.HandleFunc()方法接受兩個(gè)參數(shù)
第一個(gè)參數(shù)是http請(qǐng)求的目標(biāo)路徑"/hello",該參數(shù)值可以是字符串,也可以是字符串形式的正則表達(dá)式,第二個(gè)參數(shù)指定具體的回調(diào)方法,比如helloHandler
當(dāng)我們的程序運(yùn)行起來后,訪問-http://localhost:8080/hello,程序就會(huì)去調(diào)用hellloHandler()方法中的業(yè)務(wù)邏輯程序
-
http中g(shù)et和postdaima
-
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n318" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">//get代碼
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
resp, err := http.Get("http://www.baidu.com")
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
//post代碼
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func main() {
resp, err := http.Post("http://www.baidu.com", "application/x-www.form-urlencoded", strings.NewReader("id=1"))
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
</pre>
GO語言正則表達(dá)式
Go語言標(biāo)準(zhǔn)庫(kù)內(nèi)建提供了regexp包
-
正則字母含義
.匹配除換行符以外的任意字符
\w匹配字母或數(shù)字或下劃線或漢字
\s匹配任意的空白符
\d匹配數(shù)字
\b匹配單詞的開始或結(jié)束
^匹配字符串的開始
$匹配字符串的結(jié)束
*重復(fù)零次或更多次
+重復(fù)一次或更多次
?重復(fù)零次或一次
{n}重復(fù)n次
{n,}重復(fù)n次或更多次
{n,m}重復(fù)n到m次
捕獲(exp) 匹配exp,并捕獲文本到自動(dòng)命名的阻組里
(?<name>exp) 匹配exp,并捕獲文本到名稱為name的組里,也可以寫成(?'name'exp)
(?:exp)匹配exp,不捕獲匹配的文本,也不給此分組分配組號(hào)
-
正則函數(shù)
func Match(pattern string,b []byte)(matched bool,err error)
func MatchString(pattern string,s string)(matched bool,err error)
func MustCompile(str string)*Regexp
func(re * Regexp)FindAllString(s string,n int)[]string
-
代碼演示
-
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n372" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
"regexp"
)
func main() {
isok, _ := regexp.Match("[a-zA-Z]{3}", []byte("zhl"))
fmt.Printf("%v", isok)
reg1 := regexp.MustCompile(^z(.*)l<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n372" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;") result1 := reg1.FindAllString("zhangsan", -1) fmt.Printf("%v\n", result1) reg2 := regexp.MustCompile(
z(.{1})(.{1})(.*)l<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n372" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;")
result2 := reg2.FindAllStringSubmatch("zhangsan", -1)
fmt.Printf("%v\n", result2)
}
</pre>
-
代碼演示2
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n375" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
)
func main() {
url := "https://movie.douban.com/subject/1292052/"
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
sHtml, _ := ioutil.ReadAll(resp.Body)
// fmt.Println(string(sHtml))
reg := regexp.MustCompile(<span\s*property="v:itemreviewed">(.*)</span>
)
result := reg.FindAllStringSubmatch(string(sHtml), -1)
// fmt.Printf("%v", result)
// for _, v := range result {
// fmt.Println(v[1])
// }
fmt.Println(result[0][1])
reg1 := regexp.MustCompile(<strong\s*class="ll\s*rating_num"\s*property="v:average">(.*)</strong>
)
result1 := reg1.FindAllStringSubmatch(string(sHtml), -1)
fmt.Println(result1[0][1])
}
</pre>
GO語言Mysql與Redis
Go語言使用mysql驅(qū)動(dòng)包:https://github.com/Go-SQL-Driver/Mysql
-
使用sql.open()函數(shù)用來打開一個(gè)注冊(cè)過的數(shù)據(jù)庫(kù)驅(qū)動(dòng),Go-MySQL-Driver中注冊(cè)了mysql這個(gè)數(shù)據(jù)庫(kù)的驅(qū)動(dòng),第二個(gè)參數(shù)是DNS(Data Source Name),它是Go-MySQL-Driver定義的一些數(shù)據(jù)庫(kù)鏈接和配置信息.它支持如下格式
user@unix(/path/to/socket)/dbname?charset=utf8
user:password@tcp(localhost:5555)/dbname?chaset=utf8
-
Go語言操作mysql代碼
-
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n390" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/cost?charset=utf8")
if err != nil {
panic(err)
}
//插入
// stmt, err := db.Prepare("insert test set t_id=?,t_name=?,t_time=?")
// res, err := stmt.Exec(998, "zhangsan", "2019-01-02")
// id, err := res.LastInsertId()
//修改
// stmt, err := db.Prepare("update test set t_name=? where t_id=?")
// res, err := stmt.Exec("lisi", 999)
// id, err := res.RowsAffected()
//數(shù)據(jù)庫(kù)一主多從,從庫(kù)多是用來查詢的
//查詢
rows, err := db.Query("select * from test where t_id=999")
if err != nil {
panic(err)
}
for rows.Next() {
var t_id int
var t_name string
var t_time string
err = rows.Scan(&t_id, &t_name, &t_time)
fmt.Println(t_id, t_name, t_time)
}
//在go中創(chuàng)建的變量必須調(diào)用一次
// fmt.Println(id)
}
</pre>
Go語言使用的Redis驅(qū)動(dòng)包:https://github.com/astaxie/goredis
驅(qū)動(dòng)包需設(shè)置gopath下載使用
下載安裝redis并啟動(dòng)
-
redis的基本操作類型
string
hash
set
list
zset
-
redis代碼示例
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n412" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
"github.com/astaxie/goredis"
)
func main() {
var client goredis.Client
client.Addr = "127.0.0.1:6379"
//存入數(shù)據(jù)到redis
err := client.Set("test", []byte("hello beifeng"))
if err != nil {
panic(err)
}
//從redis獲取數(shù)據(jù)
res, err := client.Get("test")
if err != nil {
panic(err)
}
fmt.Println(string(res))
//庫(kù)中hmset方法使用
f := make(map[string]interface{})
f["name"] = "zhangsan"
f["age"] = 12
f["sex"] = "male"
err = client.Hmset("test_hash", f)
if err != nil {
panic(err)
}
//庫(kù)中 zset方法使用
_, err = client.Zadd("test_zset", []byte("beifeng"), 100)
if err != nil {
panic(err)
}
}
</pre>
-
redis命令行操作查看
- <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n417" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">//啟動(dòng)服務(wù)
redis-server.exe
//啟動(dòng)客戶端查看數(shù)據(jù)
E:\quickSorftWare\redis>redis-cli.exe
127.0.0.1:6379> get test
"hello golang"
127.0.0.1:6379> type test_hash
none
127.0.0.1:6379> type test_hash
hash
127.0.0.1:6379> hgetall test_hash
- "age"
- "12"
- "sex"
- "male"
- "name"
- "zhangsan"
127.0.0.1:6379> hgetall test_zadd
(empty list or set)
127.0.0.1:6379> type test_zadd
none
127.0.0.1:6379> type test_zset
zset
127.0.0.1:6379> zrange test_zset 0 -1
- "golang"
</pre>
簡(jiǎn)單定義使用
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n180" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
?
import "fmt"
?
type Integer struct {
value int
}
?
func (a Integer) compare(b Integer) bool {
return a.value < b.value
}
func main() {
// var a int =1
// var b int =2
a := Integer{1}
b := Integer{2}
fmt.Printf("%v", a.compare(b))
}
?</pre>
go中類的初始化
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n185" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">point:=new(Point)
point:=&Point{}
point:=&Point{x:100,y:100}
point:=Point{}</pre>-
定義與使用示例
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n190" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
import "fmt"
type Point struct {
px float32
py float32
}
func (point *Point) setxy(px, py float32) {
point.px = px
point.py = py
}
func (point *Point) getxy() (float32, float32) {
return point.px, point.py
}
func main() {
point := new(Point)
point.setxy(1.24, 4.25)
px, py := point.getxy()
fmt.Print(px, py)
}</pre>
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n190" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n194" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
?
import "fmt"
?
type Person struct {
name string
age int
}
?
func (person Person) getNameAndAge() (string, int) {
return person.name, person.age
}
?
type Student struct {
Person
speciality string
}
?
func (student Student) getSpeciality() string {
return student.speciality
}
func main() {
student := new(Student)
student.name = "zhangsan"
student.age = 26
name, age := student.getNameAndAge()
fmt.Println(name, age)
}</pre>
Go語言接口
-
在go語言中筹我,一個(gè)類只需要實(shí)現(xiàn)了接口要求的所有函數(shù),我們就說這個(gè)類實(shí)現(xiàn)了該接口
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n202" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
?
import "fmt"
//非侵入式接口
type Animal interface {
Fly()
Run()
}
type Animal2 interface {
Fly()
}
type Bird struct {
}
func (bird Bird) Fly() {
fmt.Println("bird is flying")
}
func (bird Bird) Run() {
fmt.Println("bird is Run")
}
func main() {
var animal Animal
var animal2 Animal2
bird := new(Bird)
animal = bird
animal2=bird
animal2.FLy()
animal2=animal//在GO中可以接口賦值
animal.Fly()
animal.Run()
}</pre>
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n202" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
-
在Go語言中空間口類似泛型
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n207" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">var v1=interface{}
v1=6.78
fmt.Print(v1)
//類型查詢,看接口接收的什么類型
if v, ok := v1.(float64); ok {
fmt.Println(v, ok)
}</pre>
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n207" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">var v1=interface{}
與傳統(tǒng)的系統(tǒng)級(jí)線程相比,協(xié)程的大優(yōu)勢(shì)在于其"輕量級(jí)",可以輕松創(chuàng)建上百萬個(gè)而不會(huì)導(dǎo)致系統(tǒng)資源衰竭,而線程和進(jìn)程通常多也不能超過1萬個(gè),這也是協(xié)程也叫輕量級(jí)線程的原因.
go對(duì)協(xié)程的實(shí)現(xiàn):go+函數(shù)名:啟動(dòng)一個(gè)協(xié)程執(zhí)行函數(shù)體
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n218" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
"time"
)
func test_Routine() {
fmt.Println("This is one routine!")
}
func main() {
go test_Routine()
time.Sleep(1)
}</pre>
Go并發(fā)編程之channel
-
Channel:Go語言在語言級(jí)別提供的goroutine間的通訊方式
-
聲明方式:var chanName chan ElementType
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n229" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
?
import (
"fmt"
"strconv"
)
?
func Add(x, y int, quit chan int) {
z := x + y
fmt.Println(z)
quit <- 1
}
func Read(ch chan int) {
value := <-ch
fmt.Println("value:" + strconv.Itoa(value))
}
func Write(ch chan int) {
// ch < -10
}
func main() {
chs := make([]chan int, 10)
for i := 0; i < 10; i++ {
chs[i] = make(chan int)
go Add(i, i, chs[i])
}
for _, v := range chs {
<-v
}
}</pre>
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n229" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
-
-
緩存channel
-
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n235" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
"time"
)var ch chan int
func test_channel() {
// ch := make(chan int)
ch <- 1
fmt.Println("come to end goroutline 1")
}
func main() {
ch = make(chan int, 2) //值是1和值是0的適合輸出執(zhí)行順序不同
go test_channel()
time.Sleep(2 * time.Second)
fmt.Println("running end!")
<-ch
time.Sleep(time.Second)
}
</pre>
-
-
select
是linux很早就引入的函數(shù),用來實(shí)現(xiàn)非阻塞的一種方式
-
go語言提供語言級(jí)別支持slelect關(guān)鍵字,用于處理異步IO問題
- <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n245" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">select{
case <-chan1://如果chan1成功讀取到數(shù)據(jù),則進(jìn)行該case處理語句
case chan2 <-1://如果成功向chan2寫入數(shù)據(jù),則進(jìn)行該case處理
default: //如果上面都沒有成功,則進(jìn)入default處理流程
}
</pre>
- <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n245" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">select{
-
select 三個(gè)代碼示例
-
示例1
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n251" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
"time"
)func main() {
ch := make(chan int)
go func(ch chan int) {
ch <- 1
}(ch)
// time.Sleep(2)//加這一句執(zhí)行結(jié)果不同
select {
case <-ch:
fmt.Print("come to read ch!")
default:
fmt.Print("come to default!")
}
}
</pre> -
示例2
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n254" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
//超時(shí)控制的經(jīng)典實(shí)現(xiàn)
import (
"fmt"
"time"
)func main() {
ch := make(chan int)
timeout := make(chan int, 1)
go func() {
time.Sleep(time.Second)
timeout <- 1
}()
select {
case <-ch:
fmt.Println("come to read ch!")
case <-timeout:
fmt.Println("come to timeout")
}
fmt.Print("end of code!")
}
</pre> -
示例3
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n257" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
//超時(shí)控制的經(jīng)典實(shí)現(xiàn)
import (
"fmt"
"time"
)func main() {
ch := make(chan int)
select {
case <-ch:
fmt.Println("come to read ch!")
case <-time.After(time.Second):
fmt.Println("come to timeout")
}
fmt.Print("end of code!")
}
</pre>
-
深入Go協(xié)程編程
-
協(xié)程與線程質(zhì)的不同
協(xié)程任務(wù)的業(yè)務(wù)代碼主動(dòng)要求切換,即主動(dòng)讓出執(zhí)行權(quán)
發(fā)生了IO,導(dǎo)致執(zhí)行阻塞
-
線程與協(xié)程代碼對(duì)比(線程幾百個(gè)就卡,協(xié)程十萬個(gè)都不算多)
-
Java線程
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n273" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">public class MyThread{
public static void main(String[] args){
new Thread(new Test_thread()).start();
new Thread(new Test_thread2()).start()
}
}
class Test_thread implements Runnable{
public void run(){
for(int i=0;i<100;i++){
System.out.println("thread 1:"+i)
}
}
}
class Test_thread2 implements Runnable{
public void run(){
for(int i=100;i<200;i++){
System.out.println("thread 2:+i);
}
}
}
</pre> -
Go協(xié)程
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n276" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
// "runtime"
"strconv"
"time"
)
func main() {
//協(xié)程1
go func() {
for i := 1; i < 100; i++ {
if i == 10 {
// runtime.Gosched() //主動(dòng)要求切換CPU
<-ch //遇到阻塞主動(dòng)讓出,否則一直執(zhí)行下去
}
fmt.Println("routine 1:" + strconv.Itoa(i))
}
}()
//協(xié)程2
go func() {
for i := 100; i < 200; i++ {
fmt.Println("routine 2:" + strconv.Itoa(i))
}
}()
time.Sleep(time.Second)
}
</pre>
-
使用的庫(kù)
Go語言內(nèi)置的encoding/json標(biāo)準(zhǔn)庫(kù):性能不太好
github.com/pquerna/ffjson/ffjson
json使用代碼
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n289" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
import (
"encoding/json"
"fmt"
)
type Student struct {
Name string //外部要引用要首字母大寫
Age int
}
func main() {
//對(duì)數(shù)組類型的json編碼
x := [5]int{1, 2, 3, 4, 5}
s, err := json.Marshal(x)
if err != nil {
panic(err)
}
fmt.Println(string(s))
//對(duì)map類型進(jìn)行json編碼
m := make(map[string]float64)
m["zhangsan"] = 100.4
s2, err2 := json.Marshal(m)
if err2 != nil {
panic(err2)
}
fmt.Println(string(s2))
//對(duì)對(duì)象進(jìn)行json編碼
student := Student{"zhangsan", 26}
s3, err3 := json.Marshal(student)
if err3 != nil {
panic(s3)
}
fmt.Println(string(s3))
//對(duì)s3進(jìn)行解碼
var s4 interface{}
json.Unmarshal([]byte(s3), &s4)
fmt.Printf("%v", s4)
}
</pre>
md5使用
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n292" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
import (
"crypto/md5"
"fmt"
)
func main() {
Md5Inst := md5.New()
Md5Inst.Write([]byte("zhangsan"))
Result := Md5Inst.Sum([]byte(""))
fmt.Printf("%x\n\n", Result)
}
</pre>
go語言標(biāo)準(zhǔn)庫(kù)內(nèi)建提供了net/http包
處理http請(qǐng)求:使用net/http包提供的http.ListenAndServer()方法,可以在指定的地址進(jìn)行監(jiān)聽,開啟一個(gè)HTTP.服務(wù)端該方法的原型如下:func ListenAndServe(addr string,hander Handler)error
- 該方法用于在指定的TCP網(wǎng)絡(luò)地址addr進(jìn)行監(jiān)聽,然后調(diào)用服務(wù)端處理程序來處理傳入的鏈接請(qǐng)求.該方法有兩個(gè)參數(shù):第一個(gè)參數(shù)addr即監(jiān)聽地址;第二個(gè)服務(wù)表示服務(wù)端處理程序,通常為空,這意味著服務(wù)端調(diào)用http.DefaultServeMux進(jìn)行處理,而服務(wù)端編寫的業(yè)務(wù)邏輯處理程序http.Handle()或http.HandleFunc()默認(rèn)注入http.DefaultServeMux中
處理https請(qǐng)求
func ListenAndServeTLS(addr string,certFile string,keyFile string,handler Handler) error
-
http.HandleFunc()方法接受兩個(gè)參數(shù)
第一個(gè)參數(shù)是http請(qǐng)求的目標(biāo)路徑"/hello",該參數(shù)值可以是字符串,也可以是字符串形式的正則表達(dá)式,第二個(gè)參數(shù)指定具體的回調(diào)方法,比如helloHandler
當(dāng)我們的程序運(yùn)行起來后,訪問-http://localhost:8080/hello,程序就會(huì)去調(diào)用hellloHandler()方法中的業(yè)務(wù)邏輯程序
http中g(shù)et和postdaima
-
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n318" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">//get代碼
package mainimport (
"fmt"
"io/ioutil"
"net/http"
)func main() {
resp, err := http.Get("http://www.baidu.com")
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
//post代碼
package mainimport (
"fmt"
"io/ioutil"
"net/http"
"strings"
)func main() {
resp, err := http.Post("http://www.baidu.com", "application/x-www.form-urlencoded", strings.NewReader("id=1"))
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
</pre>
正則字母含義
.匹配除換行符以外的任意字符
\w匹配字母或數(shù)字或下劃線或漢字
\s匹配任意的空白符
\d匹配數(shù)字
\b匹配單詞的開始或結(jié)束
^匹配字符串的開始
$匹配字符串的結(jié)束
*重復(fù)零次或更多次
+重復(fù)一次或更多次
?重復(fù)零次或一次
{n}重復(fù)n次
{n,}重復(fù)n次或更多次
{n,m}重復(fù)n到m次
捕獲(exp) 匹配exp,并捕獲文本到自動(dòng)命名的阻組里
(?<name>exp) 匹配exp,并捕獲文本到名稱為name的組里,也可以寫成(?'name'exp)
(?:exp)匹配exp,不捕獲匹配的文本,也不給此分組分配組號(hào)
正則函數(shù)
func Match(pattern string,b []byte)(matched bool,err error)
func MatchString(pattern string,s string)(matched bool,err error)
func MustCompile(str string)*Regexp
func(re * Regexp)FindAllString(s string,n int)[]string
代碼演示
-
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n372" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
"regexp"
)func main() {
isok, _ := regexp.Match("[a-zA-Z]{3}", []byte("zhl"))
fmt.Printf("%v", isok)
reg1 := regexp.MustCompile(^z(.*)l<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n372" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;") result1 := reg1.FindAllString("zhangsan", -1) fmt.Printf("%v\n", result1) reg2 := regexp.MustCompile(
z(.{1})(.{1})(.*)l<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n372" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;")
result2 := reg2.FindAllStringSubmatch("zhangsan", -1)
fmt.Printf("%v\n", result2)}
</pre>
-
代碼演示2
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n375" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
)func main() {
url := "https://movie.douban.com/subject/1292052/"
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
sHtml, _ := ioutil.ReadAll(resp.Body)
// fmt.Println(string(sHtml))
reg := regexp.MustCompile(<span\s*property="v:itemreviewed">(.*)</span>
)
result := reg.FindAllStringSubmatch(string(sHtml), -1)
// fmt.Printf("%v", result)
// for _, v := range result {
// fmt.Println(v[1])
// }
fmt.Println(result[0][1])
reg1 := regexp.MustCompile(<strong\s*class="ll\s*rating_num"\s*property="v:average">(.*)</strong>
)
result1 := reg1.FindAllStringSubmatch(string(sHtml), -1)
fmt.Println(result1[0][1])
}
</pre>
使用sql.open()函數(shù)用來打開一個(gè)注冊(cè)過的數(shù)據(jù)庫(kù)驅(qū)動(dòng),Go-MySQL-Driver中注冊(cè)了mysql這個(gè)數(shù)據(jù)庫(kù)的驅(qū)動(dòng),第二個(gè)參數(shù)是DNS(Data Source Name),它是Go-MySQL-Driver定義的一些數(shù)據(jù)庫(kù)鏈接和配置信息.它支持如下格式
user@unix(/path/to/socket)/dbname?charset=utf8
user:password@tcp(localhost:5555)/dbname?chaset=utf8
Go語言操作mysql代碼
-
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n390" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">package main
import (
"database/sql"
"fmt"_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/cost?charset=utf8")
if err != nil {
panic(err)
}
//插入
// stmt, err := db.Prepare("insert test set t_id=?,t_name=?,t_time=?")
// res, err := stmt.Exec(998, "zhangsan", "2019-01-02")
// id, err := res.LastInsertId()
//修改
// stmt, err := db.Prepare("update test set t_name=? where t_id=?")
// res, err := stmt.Exec("lisi", 999)
// id, err := res.RowsAffected()
//數(shù)據(jù)庫(kù)一主多從,從庫(kù)多是用來查詢的
//查詢
rows, err := db.Query("select * from test where t_id=999")
if err != nil {
panic(err)
}
for rows.Next() {
var t_id int
var t_name string
var t_time string
err = rows.Scan(&t_id, &t_name, &t_time)
fmt.Println(t_id, t_name, t_time)
}
//在go中創(chuàng)建的變量必須調(diào)用一次
// fmt.Println(id)
}
</pre>
驅(qū)動(dòng)包需設(shè)置gopath下載使用
下載安裝redis并啟動(dòng)
redis的基本操作類型
string
hash
set
list
zset
redis代碼示例
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n412" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); line-height: 1.5rem; width: inherit;">package main
import (
"fmt"
"github.com/astaxie/goredis"
)
func main() {
var client goredis.Client
client.Addr = "127.0.0.1:6379"
//存入數(shù)據(jù)到redis
err := client.Set("test", []byte("hello beifeng"))
if err != nil {
panic(err)
}
//從redis獲取數(shù)據(jù)
res, err := client.Get("test")
if err != nil {
panic(err)
}
fmt.Println(string(res))
//庫(kù)中hmset方法使用
f := make(map[string]interface{})
f["name"] = "zhangsan"
f["age"] = 12
f["sex"] = "male"
err = client.Hmset("test_hash", f)
if err != nil {
panic(err)
}
//庫(kù)中 zset方法使用
_, err = client.Zadd("test_zset", []byte("beifeng"), 100)
if err != nil {
panic(err)
}
}
</pre>
redis命令行操作查看
- <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n417" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Menlo, Monaco, "Courier New", monospace; font-size: 1.125rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: inherit; position: relative !important; color: rgb(122, 122, 122); padding: 0.5rem 1.125em; margin-bottom: 0.88em; border: 1px solid rgb(122, 122, 122); margin-top: 0px; line-height: 1.5rem; width: inherit;">//啟動(dòng)服務(wù)
redis-server.exe
//啟動(dòng)客戶端查看數(shù)據(jù)
E:\quickSorftWare\redis>redis-cli.exe
127.0.0.1:6379> get test
"hello golang"
127.0.0.1:6379> type test_hash
none
127.0.0.1:6379> type test_hash
hash
127.0.0.1:6379> hgetall test_hash- "age"
- "12"
- "sex"
- "male"
- "name"
- "zhangsan"
127.0.0.1:6379> hgetall test_zadd
(empty list or set)
127.0.0.1:6379> type test_zadd
none
127.0.0.1:6379> type test_zset
zset
127.0.0.1:6379> zrange test_zset 0 -1 - "golang"
</pre>