gin通過(guò)go build -tags實(shí)現(xiàn)json包切換及庫(kù)分析
gin的json庫(kù)分析
在github.com/gin-gonic/gin/internal/json
包下,存在兩個(gè)文件
一個(gè)是json.go
,一個(gè)是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 )
兩個(gè)文件分別定義了不同json包提供的Marshal
、Unmarshal
等方法,默認(rèn)編譯時(shí),會(huì)采用官方的json庫(kù),當(dāng)tags參數(shù)等于jsoniter時(shí),則會(huì)采用jsoniter
庫(kù)
go build -tags=jsoniter .
go build - tags
通過(guò)在代碼中增加注釋//+build xxx
時(shí),編譯時(shí)傳遞對(duì)應(yīng)的tags值,就會(huì)編譯不同的文件。
- 構(gòu)建約束以一行+build開(kāi)始的注釋。在+build之后列出了一些條件,在這些條件成立時(shí),該文件應(yīng)包含在編譯的包中;
- 約束可以出現(xiàn)在任何源文件中,不限于go文件;
- +build必須出現(xiàn)在package語(yǔ)句之前,+build注釋之后應(yīng)要有一個(gè)空行。
參考 http://chabaoo.cn/jiaoben/297334xjf.htm
以上就是gin通過(guò)go build -tags實(shí)現(xiàn)json包切換及庫(kù)分析的詳細(xì)內(nèi)容,更多關(guān)于gin切換json包的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go語(yǔ)言常見(jiàn)錯(cuò)誤之濫用getters/setters誤區(qū)實(shí)例探究
在Go語(yǔ)言編程中,恰如其分地使用getters和setters是至關(guān)重要的,過(guò)度和不適當(dāng)?shù)厥褂盟鼈兛赡軐?dǎo)致代碼冗余、可讀性差和封裝不當(dāng),在本文中,我們將深入探討如何識(shí)別濫用getter和setter的情況,以及如何采取最佳實(shí)踐來(lái)避免這些常見(jiàn)的Go錯(cuò)誤2024-01-01Golang的select多路復(fù)用及channel使用操作
這篇文章主要介紹了Golang的select多路復(fù)用及channel使用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12更換GORM默認(rèn)SQLite驅(qū)動(dòng)出現(xiàn)的問(wèn)題解決分析
這篇文章主要為大家介紹了更換GORM默認(rèn)SQLite驅(qū)動(dòng)出現(xiàn)的問(wèn)題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01深入探索Go語(yǔ)言中的高效數(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è)置消息的失效時(shí)間

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