<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ## 1.概述 項目配置是整個項目中很重要的一部分,一般項目的配置有數據庫配置,應用配置(地址,端口等),緩存配置,第三方擴展的配置,中間件配置等等,可見配置在一個項目中的地位是很重要的,但是,gin中沒有提供相關的配置管理的組件,我們可以使用go的第三方包來做配置管理,集成到gin中。 常用的第三方包有: - [ini](https://ini.unknwon.io/) - [yaml](https://gopkg.in/yaml.v2) - [viper](https://github.com/spf13/viper) - .... 本教程主要講解ini,其他的請執行[Google](https://google.com) ## 2.ini的使用 目錄結構我們使用前面推薦[項目結構](項目結構.md) - 安裝gopkg.in/ini.v1 ```shell go get gopkg.in/ini.v1 ``` - 在conf下創建app.ini配置文件 ```ini [server] address = 0.0.0.0 port = 8080 ``` - 在pkg中創建config目錄,并創建server.go,配置Server配置 ```go package config import ( "cn.sockstack/gin_demo/pkg/helper" "gopkg.in/ini.v1" ) var Server *server type server struct { Address string Port int source *ini.File } func (s *server) Load(path string) *server { var err error //判斷配置文件是否存在 exists, err := helper.PathExists(path) if !exists { return s } s.source, err = ini.Load(path) if err != nil { panic(err) } return s } func (s *server)Init() *server { //判斷配置是否加載成功 if s.source == nil { return s } s.Address = s.source.Section("server").Key("address").MustString("0.0.0.0") s.Port = s.source.Section("server").Key("port").MustInt(8080) return s } ``` 注意:`helper.PathExists(path)`是pkg/helper/util.go的工具方法,實現如下 ```go package helper import "os" func PathExists(path string) (bool, error) { _, err := os.Stat(path) if err == nil { return true, nil } if os.IsNotExist(err) { return false, nil } return false, err } ``` - 測試配置,在pkg/config目錄下創建server_test.go,并運行 ```go package config import ( "fmt" "testing" ) func TestServer(t *testing.T) { Server = (&server{}).Load("../../conf/app.ini").Init() fmt.Println(Server) if Server == nil { t.Fail() } } ``` - 測試結果,測試通過 ```go === RUN TestServer &{0.0.0.0 8081 0xc000118000} --- PASS: TestServer (0.00s) PASS Process finished with exit code 0 ``` - 在pkg/config目錄下創建init.go,初始化配置 ```go package config func init() { Server = (&server{}).Load("conf/app.ini").Init() } ``` - gin整合配置,使用快速入門的例子 ```go package main // 導入gin包 import ( "cn.sockstack/gin_demo/pkg/config" "fmt" "github.com/gin-gonic/gin" ) // 入口函數 func main() { // 初始化一個http服務對象 r := gin.Default() // 設置一個get請求的路由,url為/hello, 處理函數(或者叫控制器函數)是一個閉包函數。 r.GET("/hello", func(c *gin.Context) { // 通過請求上下文對象Context, 直接往客戶端返回一個json c.JSON(200, gin.H{ "message": "hello world", }) }) r.Run(fmt.Sprintf("%s:%d", config.Server.Address, config.Server.Port)) // 監聽并在 0.0.0.0:8080 上啟動服務 } ``` - 控制臺輸出 ```shell [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery m iddleware already attached. [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in produ ction. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode) [GIN-debug] GET /hello --> main.main.func1 (3 handlers) [GIN-debug] Listening and serving HTTP on 0.0.0.0:8080 ``` - 瀏覽器輸入localhost:8080/hello測試,響應結果 ```json { "message": "hello world" } ``` ## 3.后記 本教程使用了ini配置,其他的配置使用方式類似,有興趣的可以自行實現。 > 更多內容請關注我的博客[SOCKSTACk](https://www.sockstack.cn)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看