亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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包提供的MarshalUnmarshal等方法,默認(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)文章

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

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

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

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

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

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

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