http service reads the configuration file, cross-compiler

Because it is on the windows compiled code can not be executed on linux, you need to cross-compilation

package main

import (
    "flag"
    "gopkg.in/ini.v1"
    "log"
    "net/http"
    "strconv"
)

func main() {
    port := flag.Int("p", 8080, "服务端口")
    flag.Parse()
    if *port == 0 {
        log.Fatal("请指定端口")
    }
    cfg, err := ini.Load("my.ini")
    if err != nil {
        log.Fatal(err)
    }
    http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
        dbUser := cfg.Section("db").Key("db_user").Value()
        dbPass := cfg.Section("db").Key("db_pass").Value()
        writer.Write([]byte("<h1>" + dbUser + "</h1>"))
        writer.Write([]byte("<h1>" + dbPass + "</h1>"))
    })
    http.ListenAndServe(":"+strconv.Itoa(*port), nil)
}




Guess you like

Origin www.cnblogs.com/hualou/p/12071054.html