gin通過go build -tags實(shí)現(xiàn)json包切換及庫分析
gin的json庫分析
在github.com/gin-gonic/gin/internal/json
包下,存在兩個文件
一個是json.go
,一個是jsoniter.go
json.go
// Copyright 2017 Bo-Yi Wu. All rights reserved. // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. // +build !jsoniter package json import "encoding/json" var ( // Marshal is exported by gin/json package. Marshal = json.Marshal // Unmarshal is exported by gin/json package. Unmarshal = json.Unmarshal // MarshalIndent is exported by gin/json package. MarshalIndent = json.MarshalIndent // NewDecoder is exported by gin/json package. NewDecoder = json.NewDecoder // NewEncoder is exported by gin/json package. NewEncoder = json.NewEncoder )
jsoniter.go
// Copyright 2017 Bo-Yi Wu. All rights reserved. // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. // +build jsoniter package json import "github.com/json-iterator/go" var ( json = jsoniter.ConfigCompatibleWithStandardLibrary // Marshal is exported by gin/json package. Marshal = json.Marshal // Unmarshal is exported by gin/json package. Unmarshal = json.Unmarshal // MarshalIndent is exported by gin/json package. MarshalIndent = json.MarshalIndent // NewDecoder is exported by gin/json package. NewDecoder = json.NewDecoder // NewEncoder is exported by gin/json package. NewEncoder = json.NewEncoder )
兩個文件分別定義了不同json包提供的Marshal
、Unmarshal
等方法,默認(rèn)編譯時,會采用官方的json庫,當(dāng)tags參數(shù)等于jsoniter時,則會采用jsoniter
庫
go build -tags=jsoniter .
go build - tags
通過在代碼中增加注釋//+build xxx
時,編譯時傳遞對應(yīng)的tags值,就會編譯不同的文件。
- 構(gòu)建約束以一行+build開始的注釋。在+build之后列出了一些條件,在這些條件成立時,該文件應(yīng)包含在編譯的包中;
- 約束可以出現(xiàn)在任何源文件中,不限于go文件;
- +build必須出現(xiàn)在package語句之前,+build注釋之后應(yīng)要有一個空行。
參考 http://www.dbjr.com.cn/jiaoben/297334xjf.htm
以上就是gin通過go build -tags實(shí)現(xiàn)json包切換及庫分析的詳細(xì)內(nèi)容,更多關(guān)于gin切換json包的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go語言常見錯誤之濫用getters/setters誤區(qū)實(shí)例探究
在Go語言編程中,恰如其分地使用getters和setters是至關(guān)重要的,過度和不適當(dāng)?shù)厥褂盟鼈兛赡軐?dǎo)致代碼冗余、可讀性差和封裝不當(dāng),在本文中,我們將深入探討如何識別濫用getter和setter的情況,以及如何采取最佳實(shí)踐來避免這些常見的Go錯誤2024-01-01Golang的select多路復(fù)用及channel使用操作
這篇文章主要介紹了Golang的select多路復(fù)用及channel使用操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12更換GORM默認(rèn)SQLite驅(qū)動出現(xiàn)的問題解決分析
這篇文章主要為大家介紹了更換GORM默認(rèn)SQLite驅(qū)動出現(xiàn)的問題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01深入探索Go語言中的高效數(shù)據(jù)結(jié)構(gòu)堆
堆,作為一種基本的數(shù)據(jù)結(jié)構(gòu),以其在優(yōu)先隊(duì)列和排序算法中提供高效解決方案的能力而聞名。在本文中,我們將深入探討堆的內(nèi)部工作原理,包括其特性、實(shí)現(xiàn)細(xì)節(jié)以及在現(xiàn)代編程中的應(yīng)用2008-06-06

Golang 操作 Kafka 如何設(shè)置消息的失效時間

Go語言實(shí)現(xiàn)超時的三種方法實(shí)例