来Go2.0的泛型是这样的...
func main() { var jobs = Chan
func main() { var jobs = Chan
是否加入泛型,Go团队一直在犹豫,他们希望找到一种好的解决方案。 最近关于泛型的讨论比较激烈,总结了文档,对比了各种语言的泛型,[点击查看文档](https://docs.google.com/document/d/1vrAy9gMpMoS3uaVphB32uVXX4pi-HnNjkMEgyAHX4N4/edit?pli=1#) 如果被墙,可以[下载pdf格式](https://github.com/polaris1119/resources/raw/master/SummaryofGoGenericsDiscussions.pdf...阅读全文
package main import ( “fmt” “reflect”) type GenericSlice struct { elemType reflect.Type sliceValue reflect.Value} func (self *GenericSlice) Init(sample interface{}) { value := reflect.ValueOf(sample) self.sliceValue = reflect.MakeSlice(value.Type(), 0, 0) self.elemType = reflect.TypeOf(sample).Elem()} func (self *GenericSlice) Append(e interface{})...阅读全文
https://www.reddit.com/r/golang/comments/6nh418/generics_specific_use_cases...阅读全文
https://medium.com/@delaneygillilan/go-generics-are-you-sure-729a1715050...阅读全文
请自备梯子 https://medium.com/@watchforstock/why-i-miss-generics-in-go-9aef810a1be...阅读全文
1. 简单:没有泛型、没有异常、单一可执行文件、没有动态库 2. Goroutines:CSP、go、同步、channel 3. 错误处理 https://code.tutsplus.com/tutorials/3-things-that-make-go-different--cms-2886...阅读全文
# package slicelement Go library for finding element in slice type or operating set including union, interaction and difference. not only it supports the buildin types which includes `[]int/[]*int`, `[]float/[]*float`, `[]string/[]*string`, but also it supports `[]struct/[]*struct` . The latter is very important and convenient ## Installati...阅读全文
用go实现ConcurrentMap // 泛化的Map的接口类型 type GenericMap interface { // 获取给定键值对应的元素值。若没有对应元素值则返回nil。 Get(key interface{}) interface{} // 添加键值对,并返回与给定键值对应的旧的元素值。若没有旧元素值则返回(nil, true)。 Put(key interface{}, elem interface{}) (interface{}, bool) // 删除与给定键值对应的键值对,并返回旧的元素值。若没有旧元素值则返回nil。 Remove(key interface{}) interface{} // 清除所有的键值对。 Clear() // 获取键值对的数量。 Len(...阅读全文
用go实现ConcurrentMap// 泛化的Map的接口类型type GenericMap interface { // 获取给定键值对应的元素值。若没有对应元素值则返回nil。 Get(key interface{}) interface{} // 添加键值对,并返回与给定键值对应的旧的元素值。若没有旧元素值则返回(nil, true)。 Put(key interface{}, elem interface{}) (interface{}, bool) // 删除与给定键值对应的键值对,并返回旧的元素值。若没有旧元素值则返回nil。 Remove(key interface{}) interface{} // 清除所有的键值对。 Clear() // 获取键值对的数量。 Len() ...阅读全文
```go type syncedMap struct { *sync.RWMutex data map[interface{}]interface{} } ``` 这个结构体是想做一个同步的Map吗? data map[interface{}]interface{} 这句话是定义一个 Map<Object,Object> 吗? 怎么单独定义一个这样的Map 例如:smap:=map[interface{}]interface{}这样为什么不行...阅读全文
这里吐槽下golang没有泛型,代码重叠太多package base import ( "fmt" "math" ) type( AvlBitTree struct { data int lchild *AvlBitTree rchild *AvlBitTree bt int } IAvlBitTree interface { Less(int) bool Equal(int) bool } ) func (this *AvlBitTree) Less(data int) bool{ return data < this.data } func (this *AvlBitTree) Equal(data int) bool{ return data == this.data } func G...阅读全文
Golang没有泛型<>,但是可以通过interface{}来接收各种类型值。 如下运用切片和泛型实例: type Slice []interface{} func NewSlice() Slice { return make(Slice, 0) } func (this* Slice) Add(elem interface{}) error { for _, v := range *this { if v == elem { fmt.Printf("Slice:Add elem: %v already exist\n", elem) return ERR_ELEM_EXIST } } *this = append(*this, elem) fmt.Printf("Slice:Add ele...阅读全文
pips/generic.go //generic package pips type Generic struct { } func NewGeneric() *Generic { generic := &Generic{} return generic } func (generic *Generic) Repeat( done <-chan interface{}, args ...interface{}, ) <-chan interface{} { valueStream := make(chan interface{}) go func() { defer close(valueStream) for { for _, v := range args { select { cas...阅读全文