golang怎么用

58次阅读
没有评论

共计 4246 个字符,预计需要花费 11 分钟才能阅读完成。

这篇文章给大家分享的是有关 golang 怎么用的内容。丸趣 TV 小编觉得挺实用的,因此分享给大家做个参考,一起跟随丸趣 TV 小编过来看看吧。

1. 利用 defer、recover 来实现 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 做赋值时,会出现两个 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 的实现负担,又不增加运行时的开销,似乎也只能忍受这个奇怪的语法了。

感谢各位的阅读!关于“golang 怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-16发表,共计4246字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)