美文网首页golanggolang从0到1构建微服务
golang 交差编译与打包容器

golang 交差编译与打包容器

作者: rushui | 来源:发表于2020-04-30 11:32 被阅读0次

    build.sh

    #!/usr/bin/env bash
    CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o webdemo .
    docker build -t rushui/webdemo .
    

    Dockerfile

    FROM alpine:latest
    COPY ./webdemo /usr/local/bin/webdemo
    WORKDIR /usr/local/bin/
    EXPOSE 8080
    CMD [ "webdemo" ]
    

    webdome示例(go)

    package main
    
    import (
        "database/sql"
        "fmt"
        _ "github.com/go-sql-driver/mysql"
        "net/http"
    )
    
    func IndexHandler(w http.ResponseWriter, r *http.Request) {
        _, _ = fmt.Fprintln(w, "hello rushui")
    }
    
    func RowsHandler(w http.ResponseWriter, r *http.Request) {
        //第⼀步:打开数据库,格式是 ⽤户名:密码@/数据库名称?编码⽅式
        db, err := sql.Open("mysql", "root:root@tcp(mysql-dev:3306)/test?charset=utf8")
        if err != nil {
            fmt.Println(err)
        }
        //关闭数据库
        var id int
        var name string
        //查询数据,指定字段名,返回sql.Rows结果集
        rows, err := db.Query("select id,name from user")
        if err != nil {
            fmt.Println(err)
        }
        defer rows.Close()
        for rows.Next() {
            _ = rows.Scan(&id, &name)
            _, _ = fmt.Fprintln(w, name)
        }
    }
    
    func main() {
        http.HandleFunc("/", IndexHandler)
        http.HandleFunc("/rows", RowsHandler)
        _ = http.ListenAndServe(":8080", nil)
    }
    

    相关文章

      网友评论

        本文标题:golang 交差编译与打包容器

        本文链接:https://www.haomeiwen.com/subject/ffkzwhtx.html