http服务读取配置文件,交叉编译

因为是在windows上编译的代码,在linux上不可执行,所以需要交叉编译

http服务读取配置文件,交叉编译_第1张图片

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("

" + dbUser + "

")) writer.Write([]byte("

" + dbPass + "

")) }) http.ListenAndServe(":"+strconv.Itoa(*port), nil) }


来自为知笔记(Wiz)


你可能感兴趣的:(http服务读取配置文件,交叉编译)