golang复习

前端之家收集整理的这篇文章主要介绍了golang复习前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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 _,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,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 的实现负担,又不增加运行时的开销,似乎也只能忍受这个奇怪的语法了。

原文链接:https://www.f2er.com/go/191070.html

猜你在找的Go相关文章