欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

gin通過go build -tags實(shí)現(xiàn)json包切換及庫分析

 更新時間:2023年09月05日 14:56:18   作者:Memory  
這篇文章主要為大家介紹了gin通過go build -tags實(shí)現(xiàn)json包切換及庫分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

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)文章

  • Golang守護(hù)進(jìn)程用法示例分析

    Golang守護(hù)進(jìn)程用法示例分析

    這篇文章主要介紹了Golang守護(hù)進(jìn)程用法示例,創(chuàng)建守護(hù)進(jìn)程首先要了解go語言如何實(shí)現(xiàn)創(chuàng)建進(jìn)程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-05-05
  • Golang 操作 Kafka 如何設(shè)置消息的失效時間

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

    在使用 Golang 操作 Kafka 時,你可以使用 Sarama 庫來設(shè)置消息的失效時間,這篇文章主要介紹了Golang操作Kafka設(shè)置消息的失效時間,需要的朋友可以參考下
    2023-06-06
  • Go語言實(shí)現(xiàn)超時的三種方法實(shí)例

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

    超時在一些業(yè)務(wù)場景里非常普遍,下面這篇文章主要給大家介紹了關(guān)于Go語言實(shí)現(xiàn)超時的三種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Go語言具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-07-07
  • 最新評論