為什么要使用 easyjson 包:
因為決議json原生包用的是反射所以性能較差, 用 easyjson 比原生json包快好幾倍
安裝:
cmd 輸入下載代碼 go get -u github.com/mailru/easyjson/
坑(我的golang版本 go1.16.7 windows/amd64):
官方說下載庫后 GOPATH 路徑下的bin檔案里會有個 easyjson.exe 檔案, 結果我沒有(但有 easyjson 庫檔案)
解決:
cmd cd 到 easyjson 庫檔案下的 easyjson 檔案下, 當前檔案有個main.go檔案,
在這個目錄下cmd輸入 go build 命令, 會生成 easyjson.exe,
然后把此.exe檔案放到 GOPATH 路徑下的bin檔案里,
如果沒有bin檔案則新建一個放入即可
使用說明:
執行cdm命令: easyjson <file>.go // 前提是 GOPATH/bin/ 必須存在 easyjson.exe 可執行檔案, 這樣就會生成 easyjson_<file>.go,
//easyjson:json
type WM_SetPosition struct {
Type int `json:"type"`
UUID_cheng string `json:"uuid_cheng"` //Cheng.UUID
UUID_command string `json:"uuid_command"` //Command.UUID
ID_role int `json:"id_role"` //Role.ID
PositionNow *world.MapPosition `json:"positionNow"` //現在的位置
PositionNext *world.MapPosition `json:"positionNext"` //下一個位置
}
我這個結構體是在 socket/message.go, 所以我直接在cmd里面cd到socket下,
cdm執行 easyjson message.go, 執行完后它會在當前檔案下生成一個 easyjson_message.go 檔案,
如果一切都順利則會為為 WM_SetPosition 結構體增加了MarshalJSON、UnmarshalJSON方法
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/501234.html
標籤:Go
