随笔分类 -  golang

摘要:func LoadFile(w http.ResponseWriter, r *http.Request, p httprouter.Params) { url:= p.ByName("url") file, err:= os.Open("./" + url) if err!= nil { http 阅读全文
posted @ 2019-11-24 23:08 不骄不傲 阅读(3990) 评论(0) 推荐(0)
摘要:func UpLoad(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { r.ParseMultipartForm(32 << 20) files := r.MultipartForm.File["file"] len := 阅读全文
posted @ 2019-11-21 21:47 不骄不傲 阅读(2723) 评论(0) 推荐(0)
摘要:通过比较我们发现string.Builder和bytes.Buffer性能最好 阅读全文
posted @ 2019-08-29 22:57 不骄不傲 阅读(724) 评论(0) 推荐(0)
摘要:sync.Map详解 sync.Map是1.9才推荐的并发安全的map。 阅读全文
posted @ 2019-08-26 22:45 不骄不傲 阅读(3205) 评论(0) 推荐(0)
摘要:httprouter httprouter 是一个高性能、可扩展的HTTP路由,上面我们列举的net/http默认路由的不足,都被httprouter 实现,我们先用一个例子,认识下 httprouter 这个强大的 HTTP 路由。 安装: 在这个例子中,首先通过httprouter.New()生 阅读全文
posted @ 2019-08-21 22:50 不骄不傲 阅读(9402) 评论(0) 推荐(1)
摘要:import ( "sync" "sync/atomic" "testing" ) func TestAtomic(t *testing.T) { /* go语言提供的原子操作都是非侵入式的,它们由标准库代码包sync/atomic中的众多函数代表。 我们调用sync/atomic中的几个函数可以对几种简单的类型进行原子操作。 这些类型包括int3... 阅读全文
posted @ 2019-08-13 22:24 不骄不傲 阅读(434) 评论(0) 推荐(0)
摘要:import ( "testing" "unsafe" ) type Users struct { Age int32 Name string } func TestUnsafe(t *testing.T) { var user Users var a byte /* func Alignof(x ArbitraryType) uintptr 获取变量以多数字节... 阅读全文
posted @ 2019-08-13 21:47 不骄不傲 阅读(920) 评论(0) 推荐(0)
摘要:import ( "reflect" "testing" ) type Users struct { ID int Name string } type TestInterface interface { GetName() string } func (u *Users) UpdateName(newName string) { u.Name = newName } fu... 阅读全文
posted @ 2019-08-12 22:32 不骄不傲 阅读(3470) 评论(0) 推荐(0)
摘要:package main import ( "fmt" "net" "strings" "time" ) type Client struct { C chan string //用于发送数据的管道 Name string //用户名 Address string //IP地址 } //保存在线用户 var onLineMap map[s... 阅读全文
posted @ 2019-01-03 21:56 不骄不傲 阅读(513) 评论(0) 推荐(0)
摘要:package main import ( "fmt" "io/ioutil" "strings" ) func main() { r1 := strings.NewReader("aaa") //返回ReadCloser对象提供close函数 rc1 := ioutil.NopCloser(r1) defer rc1.Close() //ReadAll读取所有数据 p,... 阅读全文
posted @ 2019-01-03 11:24 不骄不傲 阅读(3208) 评论(0) 推荐(0)
摘要:HTTP服务端: HTTP客户端 阅读全文
posted @ 2019-01-02 22:38 不骄不傲 阅读(969) 评论(0) 推荐(0)
摘要:Timer是指定时间后执行 Ticker是按设置时间周期循环执行 阅读全文
posted @ 2018-12-11 23:21 不骄不傲 阅读(2561) 评论(0) 推荐(0)
摘要:channel可以实现线程的阻塞。 单向channel栗子 栗子 阅读全文
posted @ 2018-12-04 23:05 不骄不傲 阅读(2192) 评论(0) 推荐(0)
摘要:import ( "bufio" "fmt" "io" "os" ) //写入文件 func WriteFile(path string) { //新建文件 f, err := os.Create(path) if err != nil { fmt.Println("err=", err) return } //使用完毕需要关闭文件 defer f.Close() ... 阅读全文
posted @ 2018-11-27 23:44 不骄不傲 阅读(324) 评论(0) 推荐(0)
摘要:JSON解析内置反射方式 easyJSON高性能代码生成解析JSON 安装: go get github.com/mailru/easyjson 如果失败,可以将项目克隆到$GOPATH/src/mailru 目录下 并将包名改为easyjson,然后进入easyjson包下的easyjson目录, 阅读全文
posted @ 2018-11-27 00:14 不骄不傲 阅读(3268) 评论(0) 推荐(0)
摘要:import ( "fmt" "strconv" ) func main() { //append追加 slice := make([]byte, 0, 1024) slice = strconv.AppendBool(slice, true) slice = strconv.AppendInt(slice, 123, 10) slice = strconv.AppendQuot... 阅读全文
posted @ 2018-11-26 23:52 不骄不傲 阅读(327) 评论(0) 推荐(0)
摘要:import ( "fmt" "strings" ) func main() { //检查字符串传是否包含指定字符,返回true或者false fmt.Println(strings.Contains("hello go", "hello")) //组合切片成一个字符串 fmt.Println(strings.Join([]string{"a", "1"}, ",")) //... 阅读全文
posted @ 2018-11-26 23:42 不骄不傲 阅读(528) 评论(0) 推荐(0)