Golang的一些坑

栏目: Go · 发布时间: 7年前

内容简介:此函数退出时,会有两个goroutine被阻塞在channel上, 然而gc不会回收. 因此, 如果大量出现这种情况, 将会导致goroutine leak.操作channel都要加锁。所以略慢。因为结构体就是这么定义的 :)
  • 传给 signal.Notify 的channel必须是一个buffered channel, 否则收不到信号

  • channel默认是unbuffered channel, 因此在没有消费者之前, 放入channel的动作都会被阻塞, 例如:

func main() {
    c := make(chan int)

    for i := 0; i < 3; i++ {
        go func() {
            c <- 1
        }()
    }

    fmt.Println(<-c)
}

此函数退出时,会有两个goroutine被阻塞在channel上, 然而gc不会回收. 因此, 如果大量出现这种情况, 将会导致goroutine leak.

type hchan struct {
    qcount   uint           // total data in the queue。总数
    dataqsiz uint           // size of the circular queue。make(chan, 3)中的3.大小。也是下面的buf的大小
    buf      unsafe.Pointer // points to an array of dataqsiz elements
    elemsize uint16         // 每个元素有多大
    closed   uint32
    elemtype *_type // element type 元素是啥类型
    sendx    uint   // send index 发送的序号
    recvx    uint   // receive index 接收的序号
    recvq    waitq  // list of recv waiters 等待接收的G。sudog链表。
    sendq    waitq  // list of send waiters 等待发送的G。sudog链表。

    // lock protects all fields in hchan, as well as several
    // fields in sudogs blocked on this channel.
    //
    // Do not change another G's status while holding this lock
    // (in particular, do not ready a G), as this can deadlock
    // with stack shrinking.
    // 加锁加锁。不用atomic的原因大概是chan的缓冲数量,等待发送数量和接受者数量都不定的吧。
    // 加锁简单好用,代价就是性能略差
    lock mutex
}

操作channel都要加锁。所以略慢。

  • slice是共享底层数据的,为何?
// slice的结构体,一个指针,一个长度,一个容量
type slice struct {
    array unsafe.Pointer
    len   int
    cap   int
}

因为结构体就是这么定义的 :)

从fasthttp里学到一招避免 string[]byte 开销的方式:

package main

import (
    "bufio"
    "fmt"
    "os"
    "reflect"
    "unsafe"
)

// s2b converts string to a byte slice without memory allocation.
//
// Note it may break if string and/or slice header will change
// in the future go versions.
func s2b(s string) []byte {
    sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
    bh := reflect.SliceHeader{
        Data: sh.Data,
        Len:  sh.Len,
        Cap:  sh.Len,
    }
    return *(*[]byte)(unsafe.Pointer(&bh))
}

func main() {
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter text: ")
    hello, _ := reader.ReadString('\n')
    fmt.Println(hello)
    helloBytes := s2b(hello)
    helloBytes[1] = 'w'
    fmt.Println(hello)
}

执行一下:

$ ./tests 
Enter text: hello
hello
hwllo

不过要注意不能直接写 hello := "hello" 这样来改,因为如果显式写明字符串的话, 编译器会把它放在栈里,而不是像上面的代码一样在堆里搞。


以上所述就是小编给大家介绍的《Golang的一些坑》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C# 6.0本质论

C# 6.0本质论

[美] Mark Michaelis(马克·米凯利斯)、[美] Eric Lippert(埃里克·利珀特) / 周靖、庞燕 / 人民邮电出版社 / 2017-2-1 / 108

这是C#领域中一部广受好评的名作,作者用一种易于理解的方式详细介绍了C#语言的各个方面。全书共有21章和4个附录(其中哟2个附录从网上下载),介绍了C#语言的数据类型、操作符、方法、类、接口、异常处理等基本概念,深入讨论了泛型、迭代器、反射、线程和互操作性等高级主题,还介绍了LINQ技术,以及与其相关的扩展方法、分部方法、Lambda表达式、标准查询操作符和查询表达式等内容。每章开头的“思维导图”......一起来看看 《C# 6.0本质论》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器