惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
B
Blog RSS Feed
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
量子位
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
L
LangChain Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
IT之家
IT之家
V
Visual Studio Blog
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
I
InfoQ
D
Docker
V
V2EX

Jiajun的技术笔记

你好,2026! TiDB 源码阅读(六):TiDB Coprocessor 源码解析 性能优化的核心思想 TiDB 源码阅读(五):索引 TiDB 源码阅读(四):AST、逻辑计划、物理计划 CockroachDB Serverless Architecture podman 无故退出 Cursor Control-L (CTRL-L) Keyboard Shortcuts in Terminal Replace docker with podman Using xmonad with xfce4 A RC script for freebsd frpc 自己动手写一个k8s controller AI 会取代你的(编程)岗位吗? 自建DERP服务器提升Tailscale连接速度(使用Nginx转发) 自动升级Docker容器 再读《程序员修炼之道-从小工到专家》 让浏览器下载文件 再读《软件随想录》/《黑客与画家》/《软技能》 HTTP 压力测试中的 Coordinated Omission 2的补码 编程语言中的 context 是什么? flutter macOS 构建出错 Flatpak 使用小记 Golang CAS 操作是怎么实现的 PostgreSQL 当MQ来使用 Clash 结合 工作VPN 的网络设计 使用 PostgreSQL 搭建 JuiceFS PostgreSQL 配置优化和日志分析 有GitHub Copilot?那就可以搭建你的ChatGPT4服务 窗口函数的使用(以PG为例)
Golang的一些坑
Jiajun Huang · 2018-01-31 · via Jiajun的技术笔记
  • 传给 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.

  • for...range 语句会在执行之前执行一次。而且所有值都是拷贝,而非返回指针。

  • channel略慢,为何?

    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"这样来改,因为如果显式写明字符串的话, 编译器会把它放在栈里,而不是像上面的代码一样在堆里搞。


相关文章