完整文件代码</h2>

config.go

<pre><code>package config import ( "github.com/BurntSushi/toml" ) // Config 对应配置文件结构 type Config struct { Listen string `toml:"listen"` } // UnmarshalConfig 解析toml配置 func UnmarshalConfig(tomlfile string) (*Config, error) { c := &Config{} if _, err := toml.DecodeFile(tomlfile, c); err != nil { return c, err } return c, nil } // GetListenAddr 监听地址 func (c Config) GetListenAddr() string { return c.Listen } </code></pre>

main.go

<pre><code>package main import ( "flag" "net/http" "test/dockertest/config" "github.com/gin-gonic/gin" "github.com/go-xweb/log" ) var ( tomlFile = flag.String("config", "test.toml", "config file") ) func indexHandler(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "data": "docker test", }) } func main() { flag.Parse() // 解析配置文件 tomlConfig, err := config.UnmarshalConfig(*tomlFile) if err != nil { log.Errorf("UnmarshalConfig: err:%v\n", err) return } router := gin.New() router.GET("/", indexHandler) router.Run(tomlConfig.GetListenAddr()) } </code></pre>

test.toml

<pre><code>listen = ":8082" </code></pre>

dockerfile

<pre><code>FROM scratch MAINTAINER "hcf" WORKDIR . ADD main . ADD test.toml . EXPOSE 8082 CMD ["./main","-config=./test.toml"] </code></pre> <h2>