go语言中interface实现泛型编程
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{})...阅读全文
go channel实现
[go channel实现][1] 源至:[懒惰的程序员][2] [1]: http://alpha-blog.wanglianghome.org/2012/04/13/go-channel-implementation/ [2]: http://alpha-blog.wanglianghome.org...阅读全文
如何实现http.HandleFunc的hook
假设有下面一段程序,如何实现每次访问:8000/test时,都要先执行某个函数,完成testHandle后再执行某个函数? ``` func main() { http.HandleFunc("/test", testHandle) err := http.ListenAndServe(":8000", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } } ``...阅读全文
有没有比较好的Go版本MQTT实现?
go理论上更适合干这个啊...阅读全文
比特币(Bitcoin)的Go语言实现
关于Bitcoin的说明:https://zh-cn.bitcoin.it/wiki/%E9%A6%96%E9%A1%B5 (中文) Go语言实现:https://blog.conformal.com/btcd-a-bitcoind-alternative-written-in-go...阅读全文
Go channel 实现自增长ID
1 //autoInc.go 2 3 package autoInc 4 5 type AutoInc struct { 6 start, step int 7 queue chan int 8 running bool 9 } 10 11 func New(start, step int) (ai *AutoInc) { 12 ai = &AutoInc{ 13 start: start, 14 step: step, 15 running: true, 16 queue: make(chan int, 4), 17 } 18 19 go ai.process() 20 return 21 } 22 23 func (ai *AutoInc) process() { 24 defer fu...阅读全文
golang实现icmp中的ping功能
package main import ( "fmt" "net" "os" ) func checkSum(msg []byte) uint16 { sum := 0 len := len(msg) for i := 0; i < len-1; i += 2 { sum += int(msg[i])*256 + int(msg[i+1]) } if len%2 == 1 { sum += int(msg[len-1]) * 256 // notice here, why *256? } sum = (sum >> 16) + (sum & 0xffff) sum += (sum >> 16) var answer uint16 = uint16(^sum) return answer } ...阅读全文
一个简单的用go实现的restful 框架gorest
# gorest [](https://travis-ci.org/ejunjsh/gorest) [](http://www.babygopher.org) a restful go framework ## install ````bash go get github.com/ejunjsh/gorest ````...阅读全文
[别人code自己实现] go语言实现随机数生成器
go语言实现随机数生成器。 package main import "fmt" import "math/rand" import "time" func rand_generator() chan int{ out:=make(chan int) go func(){ for{ rand.Seed(time.Now().Unix()) out <- rand.Intn(100) } }() return out } func main(){ rand_service_handler:=rand_generator() fmt.Printf("%d\n",<-rand_service_handler) } 最好设置seed的值,这样产生的随机数相对比较“随机”点哈。至于要用哪个值作为seed...阅读全文
Read Go - Split Stack
关于split stack有许多实现方案,这里主要看下go中是如果实现的. Tw's blog [Read Go - Split Stack][1] [1]: http://totorow.herokuapp.com/posts/a38bec7638cc0d40ffe9d80246f5329...阅读全文
Martini + Jade 如何实现?
Maritini初学者,看到Martini-contrib里有个render方法,那能不能和Jade搭配使用呢...阅读全文
Go语言的接口和实现类初探(二)
扩展 上一篇讲了实现一个接口里的全部方法。 如果实现多个接口,也是很简单的,同理把他的方法实现了就行。 背景: 有一个动物的接口,他有吃和跑的动作,突然来一只鸟的动物,呀,它还会飞。 如果我们修改接口动物加上飞的动作,这样会让狗也要飞了。所以,我们把接口的方法都单独另出来。 package main import ( "fmt" ) //飞的接口 type IFly interface { Fly() } //吃的接口 type IEat interface { Eat() } //跑的接口 type IRun interface { Run() } //狗的实现类 type Dog struct { name string } func (dog *Dog) Eat() { fmt.Pri...阅读全文
基于 Go 实现的银行系统
https://speakerdeck.com/mattheath/banking-on-go-gosf-meetup-sep-201...阅读全文
Go 实现操作系统 —— 又有人挖坑了
https://github.com/achilleasa/gopher-o...阅读全文
GOLANG 实现的 fastcgi
server { listen 80; server_name go.dev; root /root/go/src/godev; index index.html; #gzip off; #proxy_buffering off; location / { try_files $uri $uri/; } location ~ /app.* { include fastcgi.conf; fastcgi_pass 127.0.0.1:9001; } try_files $uri $uri.html =404; } package main import ( "net" "net/http" "net/http/fcgi" ) type FastCGIServer struct{} func (...阅读全文
使用golang实现telnet远程登录
package main import ( "bufio" "fmt" "net" "os" "strings" ) func main() { conn, err := net.Dial("tcp", "10.71.20.161:23") if err != nil { fmt.Sprint(os.Stderr, "Error: %s", err.Error()) return } var buf [4096]byte // for { n, err := conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:...阅读全文
golang实现快排
// QuickSort package main import ( "fmt" "math/rand" "time" ) func swap(a int, b int) (int, int) { return b, a } func partition(aris []int, begin int, end int) int { pvalue := aris[begin] i := begin j := begin + 1 for j < end { if aris[j] < pvalue { i++ aris[i], aris[j] = swap(aris[i], aris[j]) } j++ } aris[i], aris[begin] = swap(aris[i], aris[begi...阅读全文
Golang的一致性哈希实现
Golang的一致性哈希实现 一致性哈希的具体介绍,可以参考:http://www.cnblogs.com/haippy/archive/2011/12/10/2282943.html 1 import ( 2 "hash/crc32" 3 "sort" 4 "strconv" 5 "sync" 6 ) 7 8 const DEFAULT_REPLICAS = 100 9 type SortKeys []uint32 10 11 func (sk SortKeys) Len() int { 12 return len(sk) 13 } 14 15 func (sk SortKeys) Less(i, j int) bool { 16 return sk[i] < sk[j] 17...阅读全文
GoLang 的 daemonize 实现
func daemonize(cmd string, args []string, pipe io.WriteCloser) error { pid, _, sysErr := syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0) if sysErr != 0 { return fmt.Errorf("fail to call fork") } if pid > 0 { if _, err := syscall.Wait4(int(pid), nil, 0, nil); err != nil { return fmt.Errorf("fail to wait for child process: %v", err) } return nil } else...阅读全文
golang的template实现自定义的循环
代码写func genlist(n string) []string { num, _ := strconv.Atoi(n) ret := make([]string, num) for i := 0; i < num; i++ { ret[i] = strconv.Itoa(i) } return ret } func output(src string, des string) bool { file, err := os.Create(des) if err != nil { fmt.Println(err) return false } t := template.New("text") if err != nil { fmt.Println(err) return false } ...阅读全文
Golang NSQ 持久实现
 lloor tar. ogar. 添加一个Lookup是怎么工作滴。 ...阅读全文
golang 实现对excel的操作
http://www.xiequan.info/golang-%E5%AE%9E%E7%8E%B0%E5%AF%B9excel%E7%9A%84%E6%93%8D%E4%BD%9C...阅读全文
channel的pub-sub模式实现
https://github.com/myself659/ChanBroker ...阅读全文
Go CopyFile 异常处理 实现
Go,CopyFile 实现 + 异常处理 package copyfile import ( "io" "os" ) func CopyFile(dst, src String) (w int64, err error) { srcFile, err := os.Open(src) if err != nil { return } defer srcFile.Close() dstFile, err := os.Open(dst) if err != nil { return } defer dstFile.Close() return io.Copy(dstFile, srcFile) ...阅读全文
golang中三种定时器的实现方式及周期定时
一、定时器的创建 golang中定时器有三种实现方式,分别是time.sleep、time.after、time.Timer 其中time.after和time.Timer需要对通道进行释放才能达到定时的效果 package main import ( "fmt" "time" ) func main() { /* 用sleep实现定时器 */ fmt.Println(time.Now()) time.Sleep(time.Second) fmt.Println(time.Now()) /* 用timer实现定时器 */ timer := time.NewTimer(time.Second) fmt.Println(<-timer.C) /* 用after实现定时器 */ fmt.Print...阅读全文
golang实现base62编码
package main import ( "fmt" "math" ) var base = []string {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9","0"} func base62encode(num int) string { baseStr...阅读全文
Golang Hash MD4
//Go标准包中只有MD5的实现 //还好,github上有MD4实现。 package main import ( "golang.org/x/crypto/md4" "encoding/hex" "fmt" ) func get_md4(buf []byte) ([] byte) { ctx := md4.New() ctx.Write(buf) return ctx.Sum(nil) } func main() { s1 := "Hello, MD4 test text" hash := get_md4([]byte(s1)) out := hex.EncodeToString(hash) fmt.Println(out) ...阅读全文
go1.5 动态密码,最简单的实现使用hmac加密
package main import ( "crypto/hmac" "crypto/sha512" "fmt" "strconv" "time" ) type Key struct { gkey string skey string date func() int64 } const ( Gkey = "What" ) func main() { K := &Key{gkey: Gkey, date: getdate} b := hmac.New(sha512.New, []byte(K.Hmac("Hello World"))) B := b.Sum(nil) offset := B[len(B)-1] & 0xf x := ((int(B[offset+1]) & 0xff) << ...阅读全文
go语言/golang实现base64加密解密
package main import ( "encoding/base64" "fmt" ) const ( base64Table = "123QRSTUabcdVWXYZHijKLAWDCABDstEFGuvwxyzGHIJklmnopqr234560178912" ) var coder = base64.NewEncoding(base64Table) func base64Encode(src []byte) []byte { return []byte(coder.EncodeToString(src)) } func base64Decode(src []byte) ([]byte, error) { return coder.DecodeString(string(src)...阅读全文
golang基于redis的分布锁实现
rt,有比较流行的包吗?类似于java的redisson这种,不需要功能太复杂,但也不要实现得太简单。...阅读全文
go run -race的底层实现
https://speakerdeck.com/kavya719/go-run-race-under-the-hoo...阅读全文
golang md5实现
package main import ( "crypto/md5" "fmt" "io") func main() { str := "123456" fmt.Print(Md5one(str)) fmt.Print("------------") fmt.Print(Md5two(str))} func Md5one(str string) (md5str string) { data := []byte(str) has := md5.Sum(data) md5str = fmt.Sprintf("%x", has) return} func Md5two(str string) (md5str string) { h := md5.New() io.WriteString(h, st...阅读全文
alias methodAliasMethod
AliasMethod 算法 Golang 实...阅读全文
WebSocket 实现原理
http://zeeyang.com/2017/07/02/websocket...阅读全文
sql server的预存储过程 go怎么实现
eg: CREATE PROC [dba] @name NVARCHAR(31) @age TOMYINT WITH ENCRYPTION A...阅读全文
60 行 Go 代码实现的 strace
结合 [A Go Programmer’s Guide to Syscalls, Liz Rice](/articles/10378) 阅读。 https://medium.com/@lizrice/strace-in-60-lines-of-go-b4b76e3ecd6...阅读全文
interface _ golang
Interfaces are named collections of methods signatures package main import ( "fmt" "math" ) type geometry interface { area() float64 perim() float64 } type square struct { width, height float64 } type circle struct { radius float64 } func (s square) area() float64 { return s.width * s.height } func (s square) perim() float64 { return 2*s.width + 2*...阅读全文
Go 实现区块链系列文章
https://jeiwan.cc/tags/bitcoin...阅读全文
go语言实现斐波数列
斐波纳契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21、……在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n>=2,n∈N*) 递归实现 package main import "fmt" func fibonacci(num int) int{ if num<2{ return 1 } return fibonacci(num-1) + fibonacci(num-2) } func main(){ for i := 0; i<10; i++{ nums := fibonacci(i) fmt.Println(nums) } } 闭包实现 package main import "fmt" func f...阅读全文
Linux下GO语言内存共享,CGO实现
package ce import ( "errors" "fmt" "os" "os/exec" ) /* #include