Daily 0131 Golang 杂乱

golang, 还是学习吧

已经两个月没有更新了

guru for vim-go #TODO

Using Go Guru 详细介绍了 guru 功能点

vim go tutorial#guru 看到对 scope 的介绍,简单易懂的教程。

Pointer analysis scope: some queries involve pointer analysis, a technique for answering questions of the form “what might this pointer point to?”.

vim-go automatically tries to be smaart and sets the current packages import path as the scope for you.

go stack

了解 golang runtime 的堆栈信息,学习如何查看 golang stack trace

Go 堆栈的理解

  • 堆栈跟踪,堆栈的参数
  • 变量是在堆 ( heap ) 还是堆栈 ( stack ) 上

go list

如何列出依赖的外部 package List external dependencies of package

go list -f '{{join .Deps "\n"}}' |  xargs go list -f '{{if not .Standard}}{{.ImportPath}}{{end}}'

或者使用 deplist

go modules 410 Gone

原因: 由于你的 go 库声明为 go1.12 格式,此时将没有 SUMDB 校验信息,因而在一个 go1.13 项目中引用这样的旧的库格式会产生校验错误,进而报错为 410 Gone。

解决办法:

  • 对于使旧格式库的人来说, 以下方式帮你顺利下载库和完成引用。
export GONOSUMDB="github.com/hedzr/errors,$GONOSUMDB"
## OR

export GOSUMDB=off
  • 对于该库的拥有者而言,下面的办法是正确的处理方案,在 go.mod 中修改库宣告的版本格式为 1.13. 例如:
module github.com/hedzr/errors

go 1.13  // go 1.12

package main
import (
  "encoding/json"
  "net/http"
)
type Profile struct {
  Name    string
  Hobbies []string
}
func main() {
  http.HandleFunc("/", foo)
  http.ListenAndServe(":3000", nil)
}
func foo(w http.ResponseWriter, r *http.Request) {
  profile := Profile{"Alex", []string{"snowboarding", "programming"}}
  js, err := json.Marshal(profile)
  if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
  }
  w.Header().Set("Content-Type", "application/json")
  w.Write(js)
}

先来段 go 代码,上面代码是返回 JSON 数据,是不是郁闷,为什么还要定义 struct Profile,没有 python/js/ 等等灵活呀

再看下 GO 怎么读 JSON 数据的

package main

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type test_struct struct {
	Test string
}

func parseGhPost(rw http.ResponseWriter, request *http.Request) {
	decoder := json.NewDecoder(request.Body)

	var t test_struct
	err := decoder.Decode(&t)

	if err != nil {
		panic(err)
	}

	fmt.Println(t.Test)
}

func main() {
	http.HandleFunc("/", parseGhPost)
	http.ListenAndServe(":8080", nil)
}

*** curl -X POST -d “{"test": "that"}” http://localhost:8080 *** 测试

Bolt是 GO 语言 key/value 存储数据库,

The API will be small and only focus on getting values and setting values. That’s it.

Bolt is currently used in high-load production environments serving databases as large as 1TB. Many companies such as Shopify and Heroku use Bolt-backed services every day.

db.Update(func(tx *bolt.Tx) error {
	b := tx.Bucket([]byte("MyBucket"))
	err := b.Put([]byte("answer"), []byte("42"))
	return err
})

更新数据库 是不是写起来很费劲呀!

db.View(func(tx *bolt.Tx) error {
	b := tx.Bucket([]byte("MyBucket"))
	v := b.Get([]byte("answer"))
	fmt.Printf("The answer is: %s\n", v)
	return nil
})

读数据一样,就不能直接 SET/GET 吗?

用 Go 自带的 http 参考这个文章 How to use sessions in Go 写了 session 机制

技术选型:如何选择一个合适的 Go http routing 呢? 那这篇文章告诉你答案:go http routing benchmark

This benchmark suite aims to compare the performance of HTTP request routers for Go by implementing the routing structure of some real world APIs. Some of the APIs are slightly adapted, since they can not be implemented 1:1 in some of the routers.

再 tornado 中异步无阻塞的执行耗时任务 原理得看,run_on_executor 装饰器对传递进来的函数封装,用 io_loop. TODO

罗列 GO 资源:

Go perfbook Writing and Optimizing Go code

Go 中文资料 杂乱,须整理成自己的知识网

fasthttp 这个性能,相比 GO 自带的 http,相差很远

ruby 点点滴滴,奇奇怪怪

[Ruby 中的 @ % # $ 等各种千奇百怪的符号的含义等』 (http://www.cnblogs.com/likeyu/archive/2012/02/22/2363912.html) @开始是实例变量、@@开始的变量是类变量 $ 开始的变量是全局变量,在程序的任何地方都可以引用。

  • 若左边最后一个表达式前带号的话,将右边多余的元素以数组的形式代入这个带的表达式中。若右边没有多余元素的话,就把空数组代入其中

?! 两个符号,一个表示布尔值,另外一个表示需要注意的

参考

  1. go modules 410 Gone https://juejin.im/post/5e0ec5d75188253aa20e858e

  2. Go Modules with Private Git Repositories https://medium.com/cloud-native-the-gathering/go-modules-with-private-git-repositories-dfe795068db4

最后修改 January 18, 2021 : fix only one h1 in page (76541b8)