久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

golang怎么用

146次閱讀
沒有評論

共計 4246 個字符,預計需要花費 11 分鐘才能閱讀完成。

這篇文章給大家分享的是有關 golang 怎么用的內容。丸趣 TV 小編覺得挺實用的,因此分享給大家做個參考,一起跟隨丸趣 TV 小編過來看看吧。

1. 利用 defer、recover 來實現(xiàn) try…catch

func Try(fun func(), handler func(interface{})) { defer func() { if err := recover(); err != nil { handler(err)
 }
 }()
 fun()
func main() { Try(func() { panic( foo)
 }, func(e interface{}) { print(e)
 })
}

2. 關于 error 的一個程序

error 是一個類型,類似于 string,error 也可以定義自己的類型

package main
import  errors 
import  fmt 
// By convention, errors are the last return value and
// have type `error`, a built-in interface.
func f1(arg int) (int, error) {
 if arg == 42 {
 // `errors.New` constructs a basic `error` value
 // with the given error message.
 return -1, errors.New(can t work with 42)
 }
 // A nil value in the error position indicates that
 // there was no error.
 return arg + 3, nil
// It s possible to use custom types as `error`s by
// implementing the `Error()` method on them. Here s a
// variant on the example above that uses a custom type
// to explicitly represent an argument error.
type argError struct {
 arg int
 prob string
func (e *argError) Error() string { return fmt.Sprintf( %d - %s , e.arg, e.prob)
func f2(arg int) (int, error) {
 if arg == 42 {
 // In this case we use ` argError` syntax to build
 // a new struct, supplying values for the two
 // fields `arg` and `prob`.
 return -1,  argError{arg,  can t work with it}
 }
 return arg + 3, nil
func main() {
 // The two loops below test out each of our
 // error-returning functions. Note that the use of an
 // inline error check on the `if` line is a common
 // idiom in Go code.
 for _, i := range []int{7, 42} { if r, e := f1(i); e != nil { fmt.Println( f1 failed: , e)
 } else { fmt.Println( f1 worked: , r)
 }
 }
 for _, i := range []int{7, 42} { if r, e := f2(i); e != nil { fmt.Println( f2 failed: , e)
 } else { fmt.Println( f2 worked: , r)
 }
 }
 // If you want to programmatically use the data in
 // a custom error, you ll need to get the error as an
 // instance of the custom error type via type
 // assertion.
 _, e := f2(42)
 if ae, ok := e.(*argError); ok { fmt.Println(ae.arg)
 fmt.Println(ae.prob)
 }
}

3. timer 和 ticker 都是可以停止的

package main
import (
 fmt 
 time 
func main() {ticker := time.NewTicker(time.Millisecond * 500)
 go func() {
 for t := range ticker.C {fmt.Println( ticker is at  , t)
 time.Sleep(time.Millisecond * 1500)
 ticker.Stop()
 fmt.Println(ticker stopped)
}
package main
import (
 fmt 
 time 
func main() {timer1 := time.NewTimer(time.Second * 2)
 -timer1.C
 fmt.Println(timer1 expired.)
 timer2 := time.NewTimer(time.Second * 1)
 go func() {
 -timer2.C
 fmt.Println(timer2 expired.)
 ok := timer2.Stop()
 if ok {fmt.Println( timer2 stopped.)
}

4. 一個比較復雜的 channel 的例子

package main
import (
 fmt 
 math/rand 
 sync/atomic 
 time 
type readOp struct {
 key int
 resp chan int
type writeOp struct {
 key int
 val int
 resp chan bool
func main() {
 var ops int64 = 0
 reads := make(chan *readOp)
 writes := make(chan *writeOp)
 go func() {var state = make(map[int]int)
 for {
 select {
 case read :=  -reads:
 read.resp  - state[read.key]
 case write :=  -writes:
 state[write.key] = write.val
 write.resp  - true
 for r := 0; r   100; r++ {go func() {
 for {
 read :=  readOp{key: rand.Intn(5),
 resp: make(chan int)}
 reads  - read
 -read.resp
 atomic.AddInt64(ops, 1)
 for w := 0; w   10; w++ {go func() {
 for {
 write :=  writeOp{key: rand.Intn(5),
 val: rand.Intn(100),
 resp: make(chan bool)}
 writes  - write
 -write.resp
 atomic.AddInt64(ops, 1)
 time.Sleep(time.Second)
 opsFinal := atomic.LoadInt64(ops)
 fmt.Println(ops: , opsFinal)
}

5. sort 包封裝了一些常用的排序方法,用起來還是很方便的

package main
import  fmt 
import  sort 
func main() {strs := []string{c ,  a ,  b}
 sort.Strings(strs)
 fmt.Println(Strings: , strs)
 ints := []int{7, 2, 4}
 sort.Ints(ints)
 fmt.Println(Ints:  , ints)
 s := sort.IntsAreSorted(ints)
 fmt.Println(Sorted:  , s)
}

6. slice 的引用特性

package main
import (
 fmt 
func main() {array := make([]int, 0, 3)
 array = append(array, 1)
 a := array
 b := array
 a = append(a, 2)
 b = append(b, 3)
 fmt.Println(a)
}

結果是什么呢?答案揭曉,輸出是“[1 3]”。

就我的理解,slice 是一個 {指向內存的指針,當前已有元素的長度,內存最大長度} 的結構體,其中只有指向內存的指針一項是真正具有引用語義的域,另外兩項都是每個 slice 自身的值。因此,對 slice 做賦值時,會出現(xiàn)兩個 slice 指向同一塊內存,但是又分別具有各自的元素長度和最大長度。程序里把 array 賦值給 a 和 b,所以 a 和 b 會同時指向 array 的內存,并各自保存一份當前元素長度 1 和最大長度 3。之后對 a 的追加操作,由于沒有超出 a 的最大長度,因此只是把新值 2 追加到 a 指向的內存,并把 a 的“當前已有元素的長度”增加 1。之后對 b 進行追加操作時,因為 a 和 b 各自擁有各自的“當前已有元素的長度”,因此 b 的這個值依舊是 1,追加操作依舊寫在 b 所指向內存的偏移為 1 的位置,也就復寫了之前對 a 追加時寫入的 2。

為了讓 slice 具有引用語義,同時不增加 array 的實現(xiàn)負擔,又不增加運行時的開銷,似乎也只能忍受這個奇怪的語法了。

感謝各位的閱讀!關于“golang 怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

正文完
 
丸趣
版權聲明:本站原創(chuàng)文章,由 丸趣 2023-08-16發(fā)表,共計4246字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發(fā)布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 蒲江县| 遂川县| 阜城县| 弥勒县| 宜宾市| 富宁县| 开原市| 洛阳市| 阆中市| 石台县| 淮南市| 子长县| 商南县| 枣庄市| 鄱阳县| 西安市| 阜南县| 德保县| 灌南县| 得荣县| 孟州市| 井研县| 炎陵县| 武义县| 白银市| 密山市| 辽源市| 会宁县| 东阿县| 湘乡市| 山东省| 临澧县| 南岸区| 哈尔滨市| 两当县| 万荣县| 孙吴县| 碌曲县| 榕江县| 宜春市| 张北县|