Replies: 30 comments 48 replies
-
select 定义A "select" statement chooses which of a set of possible send or receive operations will proceed. It looks similar to a "switch" statement but with the cases all referring to communication operations. 知识点
面向信仰编程
func main() {
ch := make(chan int)
go func() {
for range time.Tick(1 * time.Second) {
ch <- 0
}
}()
for {
select {
case <-ch:
println("case1")
case <-ch:
println("case2")
}
}
} for-select时break、continue、goto
|
Beta Was this translation helpful? Give feedback.
-
rangeMap 类型对于map类型遍历时,如果map没有初始化,只是map类型零值 package main
import (
"errors"
"fmt"
)
func main() {
fmt.Println("Hello, 世界")
succ, fail := t(false)
fmt.Println(succ, fail, succ == nil, fail == nil, len(succ), len(fail))
fmt.Printf("%p %p\n", succ, fail)
for k, v := range succ {
fmt.Println(k, v)
}
}
func t(ok bool) (succ map[string]int64, fail map[string]error) {
if ok {
succ["1"] = 1
fail["2"] = errors.New("2")
}
return
}
|
Beta Was this translation helpful? Give feedback.
-
for 循环A "for" statement specifies repeated execution of a block. There are three forms: The iteration may be controlled by a single condition, a "for" clause, or a "range" clause. For statements with single condition
For statements with for clause
分号分隔三段:
特殊用法
i := 0
for ; ; i++ {
fmt.Println(i)
if i > 10 {
break
}
}
for i := 0; ; i++ {
if i > 10 {
break
}
fmt.Println(i)
}
for i := 0; ; {
fmt.Println(i)
if i > 10 {
break
}
i++
} For statements with range clause
|
Beta Was this translation helpful? Give feedback.
-
Function函数A function type denotes the set of all functions with the same parameter and result types. The value of an uninitialized variable of function type is nil. |
Beta Was this translation helpful? Give feedback.
-
slice切片package main
import (
"fmt"
"sync"
cmap "github.com/orcaman/concurrent-map"
"github.com/panjf2000/ants/v2"
)
func main() {
pool, _ := ants.NewPool(500)
defer pool.Release()
fmt.Println("-", pool.Cap(), pool.Free(), pool.Running(), pool.Waiting())
fn := func(i int, succ, fail cmap.ConcurrentMap, wg *sync.WaitGroup) func() {
return func() {
defer wg.Done()
fmt.Println(i)
succ.Set(fmt.Sprintf("%d", i), i)
fail.Set(fmt.Sprintf("%d", i), i)
fmt.Println(&succ)
fmt.Printf("succ %p, fail %p", &succ, &fail)
fmt.Println("=", pool.Cap(), pool.Free(), pool.Running(), pool.Waiting())
}
}
succ := cmap.New()
fail := cmap.New()
fmt.Println(&succ)
fmt.Printf("succ %p %p, fail %p %p", succ, &succ, fail, &fail)
wg := sync.WaitGroup{}
for i, _ := range make([]int, 2000) {
wg.Add(1)
pool.Submit(fn(i, succ, fail, &wg))
}
wg.Wait()
fmt.Printf("succ %p, fail %p", &succ, &fail)
fmt.Println("*", pool.Cap(), pool.Free(), pool.Running(), pool.Waiting())
chk := map[int]struct{}{}
for i, _ := range make([]int, 2000) {
chk[i] = struct{}{}
}
fmt.Println(succ.Count())
for i, v := range succ.Items() {
fmt.Println(i, v)
}
fmt.Println("++++++++++")
fmt.Println(fail.Count())
for i, v := range fail.Items() {
fmt.Println(i, v)
}
} |
Beta Was this translation helpful? Give feedback.
-
new和make 定义
new 分配内存https://go.dev/ref/spec#Allocation make 创建slice、map和channel |
Beta Was this translation helpful? Give feedback.
-
iota常量iota 文档 https://go.dev/ref/spec#Iota
Go 为什么要设计 iota 常量?
iota 常用于以下的场景:
示例const (
c0 = iota // c0 == 0
c1 = iota // c1 == 1
c2 = iota // c2 == 2
)
const (
a = 1 << iota // a == 1 (iota == 0)
b = 1 << iota // b == 2 (iota == 1)
c = 3 // c == 3 (iota == 2, unused)
d = 1 << iota // d == 8 (iota == 3)
)
const (
u = iota * 42 // u == 0 (untyped integer constant)
v float64 = iota * 42 // v == 42.0 (float64 constant)
w = iota * 42 // w == 84 (untyped integer constant)
)
const x = iota // x == 0
const y = iota // y == 0
const (
bit0, mask0 = 1 << iota, 1<<iota - 1 // bit0 == 1, mask0 == 0 (iota == 0)
bit1, mask1 // bit1 == 2, mask1 == 1 (iota == 1)
_, _ // (iota == 2, unused)
bit3, mask3 // bit3 == 8, mask3 == 7 (iota == 3)
) |
Beta Was this translation helpful? Give feedback.
-
go get 问题fatal: Authentication failed for 'https://xxx.com/xx' 解决 git config --global --add url."[email protected]:".insteadOf "https://github.com/" |
Beta Was this translation helpful? Give feedback.
-
go高效编码工具转换和生成各种golang代码,提高编码效率 |
Beta Was this translation helpful? Give feedback.
-
contextPackage context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes. |
Beta Was this translation helpful? Give feedback.
-
Generics(泛型) Tutorial: Getting started with generics
|
Beta Was this translation helpful? Give feedback.
-
nilnil is a predeclared identifier representing the zero value for a pointer, channel, func, interface, map, or slice type. var nil Type // Type must be a pointer, channel, func, interface, map, or slice type
|
Beta Was this translation helpful? Give feedback.
-
闭包
问题 1:如何实现闭包? 闭包又是什么?你可以想象一下,在一个函数中存在对外来标识符的引用。所谓的外来标识符,既不代表当前函数的任何参数或结果,也不是函数内部声明的,它是直接从外边拿过来的。 还有个专门的术语称呼它,叫自由变量,可见它代表的肯定是个变量。实际上,如果它是个常量,那也就形成不了闭包了,因为常量是不可变的程序实体,而 闭包体现的却是由“不确定”变为“确定”的一个过程。 我们说的这个函数(以下简称闭包函数)就是因为引用了自由变量,而呈现出了一种“不确定”的状态,也叫“开放”状态。 也就是说,它的内部逻辑并不是完整的,有一部分逻辑需要这个自由变量参与完成,而后者到底代表了什么在闭包函数被定义的时候却是未知的。 即使对于像 Go 语言这种静态类型的编程语言而言,我们在定义闭包函数的时候最多也只能知道自由变量的类型。 在我们刚刚提到的genCalculator函数内部,实际上就实现了一个闭包,而genCalculator函数也是一个高阶函数。 func genCalculator(op operate) calculateFunc {
return func(x int, y int) (int, error) {
if op == nil {
return 0, errors.New("invalid operation")
}
return op(x, y), nil
}
} genCalculator函数只做了一件事,那就是定义一个匿名的、calculateFunc类型的函数并把它作为结果值返回。 |
Beta Was this translation helpful? Give feedback.
-
go module |
Beta Was this translation helpful? Give feedback.
-
Go Code Review Comments |
Beta Was this translation helpful? Give feedback.
-
sortPackage sort provides primitives for sorting slices and user-defined collections. |
Beta Was this translation helpful? Give feedback.
-
reflect 反射reflect包实现了运行时反射,允许程序操作任意类型的对象。典型的用法就是接受一个 调用 关于go里面反射的介绍,参考“反射定律”:https://golang.org/doc/articles/laws_of_reflection.html |
Beta Was this translation helpful? Give feedback.
-
interface 接口 |
Beta Was this translation helpful? Give feedback.
-
unsafe |
Beta Was this translation helpful? Give feedback.
-
struct 结构体结构体是一组命名元素(称为字段)的序列,每个字段都有名称和类型。字段名可以显式指定(IdentifierList)或隐式指定(EmbeddedField)。在结构体中,非空字段名必须是唯一的。 |
Beta Was this translation helpful? Give feedback.
-
Method 方法A method is a function with a receiver. A method declaration binds an identifier, the method name, to a method, and associates the method with the receiver's base type.
|
Beta Was this translation helpful? Give feedback.
-
Go 内存模型 |
Beta Was this translation helpful? Give feedback.
-
Go 垃圾回收 |
Beta Was this translation helpful? Give feedback.
-
channel 类型 |
Beta Was this translation helpful? Give feedback.
-
defer 语句 |
Beta Was this translation helpful? Give feedback.
-
panic · recover |
Beta Was this translation helpful? Give feedback.
-
Time 时间Package time provides functionality for measuring and displaying time. The calendrical calculations always assume a Gregorian calendar, with no leap seconds. |
Beta Was this translation helpful? Give feedback.
-
Organizing a Go module |
Beta Was this translation helpful? Give feedback.
-
Declarations and scope 声明和作用域 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
go
Beta Was this translation helpful? Give feedback.
All reactions