Go介面掃盲

2021-10-07 03:30:49 字數 2507 閱讀 4855

var r rune

='文'

fmt.

printf

("%#u\n"

, r)

//通過range可以遍歷乙個字串中所有的rune:

const nihongo =

"one world世界大同"

for index, runevalue :=

range nihongo

//因為字串是以utf-8編碼的,通過輸出可以看到ascii字母只用乙個位元組,而這幾個中文漢字每個漢字用了3個位元組。

//要想獲得字串中包含幾個字元(rune),下面的方法是不對的,它返回的是字元處內部的字slice的長度((9 + 4*3 =21):

const str =

"one world世界大同"

fmt.

println

(len

(str)

)

var vs =

"hello xiaoxiao"

const s = vs //錯誤!!

var v1 int

= ivar v2 float32

= ivar v3 complex64

= i

type myfile struct

func

main()

靜態型別和動態型別
var x inte***ce

// x 為零值 nil,靜態型別為 inte***ce{}

var v *t // v 為零值 nil, 靜態型別為 *t

x =42

// x 的值為 42,動態型別為int, 靜態型別為inte***ce{}

x = v // x 的值為 (*t)(nil), 動態型別為 *t, 靜態型別為 *t

命名型別和未命名型別
// x1 x2 型別相同

// y y2 型別不同

var x struct

type foo struct

var y foo

var x2 struct

type bar struct

var y2 bar

x :=

[...

]int

p :=

&x[0

]//p = p + 1

index2pointer := unsafe.

pointer

(uintptr

(unsafe.

pointer

(p))

+ unsafe.

sizeof

(x[0])

)p =(*

int)

(index2pointer)

//x[1]

fmt.

printf

("%d\n"

,*p)

//2

type arbitrarytype int

// shorthand for an arbitrary go type; it is not a real type

type pointer *arbitrarytype

pointer代表指向任意型別的指標,它有四個獨有的操作:

任意型別的指標可以被轉換成乙個 pointer物件.

相反乙個pointer也可以轉換成任意型別的指標.

乙個uintptr可以轉換成乙個pointer.

相反乙個pointer可以轉換成uintptr.

switch x :=f(

); x>

0

var a [

]int

var c, c1, c2, c3, c4 chan

intvar i1, i2 int

select

else

case a[f(

)]=<-c4:

// same as:

// case t := <-c4

// a[f()] = t

default

:print

("no communication\n")}

for}

select

// block forever

c1 :=

make

(chan

string,1

)gofunc()

()//執行超時case

select

//執行c2

c2 :=

make

(chan

string,1

)gofunc()

()select

網路介面掃盲

1 網路介面的命名 這裡並不存在一定的命名規範,但網路介面名字的定義一般都是要有意義的。例如 eth0 ethernet的簡寫,一般用於乙太網介面。wifi0 wifi是無線區域網,因此wifi0一般指無線網路介面。ath0 atheros的簡寫,一般指atheros晶元所包含的無線網路介面。lo ...

Go 介面,介面繼承

demo.go 介面繼承 package main import fmt 父介面 type humen inte ce 子介面 type person inte ce 學生類 type student struct 學生類的方法 讓學生類符合父介面的規則 func stu student sayhe...

Go 語言介面

go 語言提供了另外一種資料型別即介面,它把所有的具有共性的方法定義在一起,任何其他型別只要實現了這些方法就是實現了這個介面。例項 定義介面 type inte ce name inte ce 定義結構體 type struct name struct 實現介面方法 func struct name...