You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
conversion of a Pointer to a uintptr (but not back to Pointer)
Converting a Pointer to a uintptr produces the memory address of the value pointed at, as an integer. The usual use for such a uintptr is to print it.
Conversion of a uintptr back to Pointer is not valid in general.
Conversion of a Pointer to a uintptr and back, with arithmetic.
p=unsafe.Pointer(uintptr(p) +offset)
// equivalent to f := unsafe.Pointer(&s.f)f:=unsafe.Pointer(uintptr(unsafe.Pointer(&s)) +unsafe.Offsetof(s.f))
// equivalent to e := unsafe.Pointer(&x[i])e:=unsafe.Pointer(uintptr(unsafe.Pointer(&x[0])) +i*unsafe.Sizeof(x[0]))
Unlike in C, it is not valid to advance a pointer just beyond the end of its original allocation:
// INVALID: end points outside allocated space.varsthingend=unsafe.Pointer(uintptr(unsafe.Pointer(&s)) +unsafe.Sizeof(s))
// INVALID: end points outside allocated space.b:=make([]byte, n)
end=unsafe.Pointer(uintptr(unsafe.Pointer(&b[0])) +uintptr(n))
Conversion of a Pointer to a uintptr when calling syscall.Syscall.
// validsyscall.Syscall(SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(p)), uintptr(n))
// not valid// INVALID: uintptr cannot be stored in variable// before implicit conversion back to Pointer during system call.u:=uintptr(unsafe.Pointer(p))
syscall.Syscall(SYS_READ, uintptr(fd), u, uintptr(n))
参考资料
(usage:: 默认 nil)
slice 与 nil
map 与 nil
channel 与 nil
nil 与 interface
空结构
(usage:: defer)
使用规则
defer 与 recovery
清理资源,避免遗忘
effective go
格式化
注释文档 (godoc/go doc)
Name
defer
分配对象
Array
Slices
Maps
删除:delete(m, key)。key 不存在时也是安全的。
Printing
…
常量 (Constants)
init 函数
值 (values) 和指针 (pointers)
接口 (interfaces)
类型转换
嵌入
并发
不要通过共享内存来交互通过交互来共享内存。
尽量使用 channel 通信。
Goroutines
Channels
并行 (Parallelization)
Error & Panic
Recover
interface
how interface
空接口
区分接口存储类型的方法
接收者为值类型和指针类型
reflection
时间
time.Time
和time.Duration
两个类型flag
通过time.ParseDuration
支持了time.Duration
encoding/json
中也能把time.Time
编码成 RFC 3339 的格式database/sql
也支持把DATATIME
或TIMESTAMP
类型转成time.Time
gopkg.in/yaml.v2
也支持time.Time
、time.Duration
和 RFC 3339 格式逃逸
性能 tips
strconv.Itoa()
会比fmt.Sprintf()
要快一倍左右String
转成[]Byte
。这个转换会导致性能下降。append()
请先把 slice 的容量很扩充到位,这样能避免内存重新分享以及系统自动按 2 的 N 次方幂进行扩展但又用不到,从而浪费内存。StringBuffer
或是StringBuild
来拼接字符串,会比使用+
或+=
性能高三到四个数量级。sync.WaitGroup
来同步分片操作sync.Pool
来重用对象。sync/Atomic
包。bufio.NewWrite()
和bufio.NewReader()
能带来更高的性能。regexp.Compile()
编译正则表达式。性能会得升两个数量级。builder 模式
unsafe.Pointer 和 uintptr 类型
#type/golang #public
The text was updated successfully, but these errors were encountered: