这是我参与「第五届青训营」伴学笔记创作活动的第一天 性能优化指南 slice 预分配内存 map 预分配
func set(size int){
data:=make( []int ,size )
}
字符串拼接高性能示例:
s := "123"
b := "456"
var ss strings.Builder
ss.WriteString(s)
ss.WriteString(b)
fmt.Println(ss.String())
go-http
浏览器的请求数据转换为go代码curlconverter.com/go/ json转为go struct www.senlt.cn/json2go/ package main
import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http" "os" "strings" )
type AutoGenerated struct { Rc int json:"rc" Wiki struct { KnownInLaguages int json:"known_in_laguages" Description struct { Source string json:"source" Target interface{} json:"target" } json:"description" ID string json:"id" Item struct { Source string json:"source" Target string json:"target" } json:"item" ImageURL string json:"image_url" IsSubject string json:"is_subject" Sitelink string json:"sitelink" } json:"wiki" Dictionary struct { Prons struct { EnUs string json:"en-us" En string json:"en" } json:"prons" Explanations []string json:"explanations" Synonym []string json:"synonym" Antonym []string json:"antonym" WqxExample [][]string json:"wqx_example" Entry string json:"entry" Type string json:"type" Related []interface{} json:"related" Source string json:"source" } json:"dictionary" } type po struct { Trans_type string json:"trans_type" Source string json:"source" }
func main() {
var jsons = new(AutoGenerated)
var pos = new(po)
client := &http.Client{}
//`{"trans_type":"en2zh","source":"good"}`
sum := os.Args[1]
pos.Source = sum
pos.Trans_type = "en2zh"
// var data = strings.NewReader(`{"trans_type":"en2zh","source":"` + s + `"}`)
js, _ := json.Marshal(&pos)
data := strings.NewReader(string(js))
req, err := http.NewRequest("POST", "https://api.interpreter.caiyunai.com/v1/dict", data)
if err != nil {
log.Fatal(err)
}
req.Header.Set("authority", "api.interpreter.caiyunai.com")
req.Header.Set("accept", "application/json, text/plain, */*")
req.Header.Set("accept-language", "zh-CN,zh;q=0.9")
req.Header.Set("app-name", "xy")
req.Header.Set("content-type", "application/json;charset=UTF-8")
req.Header.Set("device-id", "")
req.Header.Set("origin", "https://fanyi.caiyunapp.com")
req.Header.Set("os-type", "web")
req.Header.Set("os-version", "")
req.Header.Set("referer", "https://fanyi.caiyunapp.com/")
req.Header.Set("sec-ch-ua", `"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"`)
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", `"macOS"`)
req.Header.Set("sec-fetch-dest", "empty")
req.Header.Set("sec-fetch-mode", "cors")
req.Header.Set("sec-fetch-site", "cross-site")
req.Header.Set("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
req.Header.Set("x-authorization", "token:qgemv4jr1y38jyq6vhvi")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
json.Unmarshal(bodyText, &jsons)
fmt.Println(jsons.Dictionary.Explanations)
}
go 并发与通道,依赖
CSP --提倡通过通信共享内存
不同的go-channel可共享goroutine内存空间
复制代码
对于channel生产与消费
生产者可能逻辑性较为简单,消费者逻辑较为复杂这就导致了生产比消费更快。为了不影响生产者的执行效率,消费者可用带缓冲的消费通道。
复制代码
对于并发安全
sync.Mutex .lock() || .unlock() 加锁 解锁
为了并发效果理想,并发安全可以在临界区域加锁
go func(){}时 time.sleep防止提前退出并不优雅,可以用sync.waitgroup .add .done .wait来操作