Go-HelloWord

go入门

docker打包构建部署

  1. 创建一个项目目录和两个文件,结构如下

    1
    2
    3
    go-hello
    |-hello.go
    |-Dockerfile
  2. 文件内容分别如下

    hello.go文件内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    package main

    import (
    "fmt"
    "log"
    "net/http"
    )

    func hello(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello World")
    }

    func main() {
    http.HandleFunc("/", hello)
    if err := http.ListenAndServe(":8080", nil); err != nil {
    log.Fatal(err)
    }
    }

    Dockerfile文件内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    FROM golang:1.21.3-alpine AS builder
    WORKDIR /build
    ADD . /build
    RUN go build -o hello ./hello.go

    FROM alpine
    WORKDIR /build
    COPY --from=builder /build/hello /build/hello
    EXPOSE 8080
    CMD ["./hello"]
  3. 在项目跟目录(go-hello),执行docker build -t hello:latest .进行镜像打包

  4. 运行镜像docker run -p 8080:8080 hello:latest然后访问http://127.0.0.1:8080/

手动测试

  1. 在项目跟目录执行go build -o hello ./hello.go生成hello可执行文件
  2. 执行./hello,然后访问http://127.0.0.1:8080/

导入github包

  1. 在项目跟目录执行go mod init go-hello,会生成一个go.modgo.sum的文件,文件内容如下:

    go.mod

    1
    2
    3
    4
    module qiniu_go
    go 1.21.3
    require github.com/qiniu/go-sdk/v7 v7.19.0
    require golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect

    go.sum

    1
    2
    3
    github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
    github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
    .....
  2. 在项目跟目录执行go mod tidy,删除错误或者不使用的modules

实战

写一个接口将接口的json数据保存到七牛云,支持修改

main.go,dockerfile根据hello的例子进行修改即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"

"github.com/qiniu/go-sdk/v7/auth"
"github.com/qiniu/go-sdk/v7/storage"
)

func upload(writer http.ResponseWriter, request *http.Request) {
// 检查请求方法是否为POST
if request.Method != http.MethodPost {
http.Error(writer, "Invalid request method", http.StatusMethodNotAllowed)
return
}

// 读取请求体
body, err := io.ReadAll(request.Body)
if err != nil {
http.Error(writer, "Error reading request body", http.StatusInternalServerError)
return
}

// 解析JSON数据
var jsonData map[string]interface{}
err = json.Unmarshal(body, &jsonData)
if err != nil {
http.Error(writer, "Error decoding JSON", http.StatusBadRequest)
return
}

// 打印接收到的JSON数据
fmt.Printf("Received JSON: %+v\n", jsonData)

// 可选:将JSON数据转为字节
data, err := json.Marshal(jsonData)
if err != nil {
http.Error(writer, "Error encoding JSON", http.StatusInternalServerError)
return
}

accessKey := ""
secretKey := ""
bucket := "test"
keyToOverwrite := "3D/3d.json"

putPolicy := storage.PutPolicy{
Scope: fmt.Sprintf("%s:%s", bucket, keyToOverwrite),
}
mac := auth.New(accessKey, secretKey)
upToken := putPolicy.UploadToken(mac)

cfg := storage.Config{}
// 空间对应的机房
cfg.Region = &storage.ZoneHuanan
// 是否使用https域名
cfg.UseHTTPS = true
// 上传是否使用CDN上传加速
cfg.UseCdnDomains = false

formUploader := storage.NewFormUploader(&cfg)
ret := storage.PutRet{}
putExtra := storage.PutExtra{
Params: map[string]string{
"x:name": "github logo",
},
}
//data := []byte("hello, this is qiniu cloud")
dataLen := int64(len(data))
qErr := formUploader.Put(context.Background(), &ret, upToken, keyToOverwrite, bytes.NewReader(data), dataLen, &putExtra)
if err != nil {
fmt.Println("err:", qErr)
return
}
fmt.Println(ret.Key, ret.Hash)
fmt.Fprintf(writer, "https://qiniu.xx.com/"+keyToOverwrite)
}

func main() {
fmt.Println("service start")
http.HandleFunc("/upload", upload)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}

}