信号:在功能结束时被杀死

时间:2017-07-24 14:49:33

标签: go go-gin

使用go-gin我有这个功能来接收已发布的笑话并将它们保存到数据库中:

func PostJoke(c *gin.Context) {
    var err error

    userId, userName, userAvatar := shared.GetUserInfo(c)

    if userId == 0 {
        c.JSON(403, gin.H{"error": "not authorized"})
    }

    title := c.PostForm("title")
    content := c.PostForm("content")
    nsfwSt := c.PostForm("nsfw")
    nsfw, err := strconv.Atoi(nsfwSt)
    if err != nil {
        log.Panic(err)
        c.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})
    }

    file, err := c.FormFile("file")
    if err != nil {
        log.Println(err)
        c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
        return
    }
    dir, err := os.Getwd()
    if err != nil {
        log.Fatal(err)
    }
    filename := path.Join(shared.RandString(6) + path.Ext(file.Filename))
    dest := dir + "/media/photos/" + filename
    if err := c.SaveUploadedFile(file, dest); err != nil {
        c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
        return
    }
    _, err = shared.Dbmap.Exec("INSERT INTO joke (user_id, username, user_avatar, title, content, photo, nsfw) VALUES (?, ?, ?, ?, ?, ?, ?)", userId, userName, userAvatar, title, content, filename, nsfw)
    if err != nil {
        log.Println(err)
        c.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})
    }

    fmt.Println("end of function")
    c.JSON(http.StatusOK, gin.H{"sccess": "joke is created"})

}

该功能按预期工作:保存数据并返回"sccess": "joke is created" 但最后我在终端收到了这个神秘的错误:

signal: killed

并且不再回复。

我想知道是什么导致这种情况以及如何解决这个问题?

更新 go version go1.8.3 linux/amd64

这是我的go env

me@pc:~$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/me/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build842411537=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

当我尝试使用sudo go install -a 重建所有包时,我得到:

main.go:8:2: cannot find package "github.com/gin-gonic/contrib/sessions" in any of:
    /usr/lib/go-1.6/src/github.com/gin-gonic/contrib/sessions (from $GOROOT)
    ($GOPATH not set)
main.go:9:2: cannot find package "github.com/gin-gonic/gin" in any of:
    /usr/lib/go-1.6/src/github.com/gin-gonic/gin (from $GOROOT)
    ($GOPATH not set)
main.go:4:2: cannot find package "proj/controller" in any of:
    /usr/lib/go-1.6/src/proj/controller (from $GOROOT)
    ($GOPATH not set)
main.go:5:2: cannot find package "proj/middleware" in any of:
    /usr/lib/go-1.6/src/proj/middleware (from $GOROOT)
    ($GOPATH not set)

0 个答案:

没有答案