Skip to content

Instantly share code, notes, and snippets.

Avatar
👻
What's happening?

legendtkl legendtkl

👻
What's happening?
View GitHub Profile
@legendtkl
legendtkl / README.md
Last active Dec 16, 2020
websocket demo
View README.md

How to Run

go run server.go

and in another terminal

go run client.go
View closure1.go
package main
import (
"fmt"
)
func main() {
for i:=0; i<10; i++ {
go fmt.Println(i)
}
View funcCallbyReflect.go
package main
import (
"reflect"
"fmt"
)
type Test struct {
fn interface{}
args []reflect.Value
@legendtkl
legendtkl / proxy.go
Created Aug 18, 2016
simple golang tcp proxy (forward request)
View proxy.go
package main
import (
"fmt"
"net"
"io"
)
func main() {
//http.HandleFunc("/", handler)
@legendtkl
legendtkl / copyStructFields.go
Last active Aug 1, 2016
在golang 做orm的时候经常有这样一个场景:两个struct有部分相同的成员(名字和类型)。如果拷贝struct中这些字段的话逐个拷贝就会比较耗费时间,下面代码使用reflect实现了拷贝。
View copyStructFields.go
func CopyStruct(src, dst interface{}) {
sval := reflect.ValueOf(src).Elem()
dval := reflect.ValueOf(dst).Elem()
for i := 0; i < sval.NumField(); i++ {
value := sval.Field(i)
name := sval.Type().Field(i).Name
dvalue := dval.FieldByName(name)
if dvalue.IsValid() == false {
View golang_gist.go
//error handle
//when exit, print stack out
func foo() {
if err := recover(); err != nil {
debug.PrintStack()
log.Error("Error: ", err)
}()
}