将无服务器功能部署到golang的digitalocean

zujrkrfu  于 2022-12-07  发布在  Go
关注(0)|答案(1)|浏览(135)

我正在尝试在数字海洋无服务器函数中构建一个CRUD。我正在使用sqlite3进行测试并插入到表中。当我尝试将其部署到生产中时,我遇到了错误。以下是我的代码:

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/mattn/go-sqlite3"
)

func Main(args map[string]interface{}) map[string]interface{} {
db, err := sql.Open("sqlite3", "./data.db")
checkErr(err)

// insert
stmt, err := db.Prepare("INSERT INTO userinfo(username, departname, created) values(?,?,?)")
checkErr(err)

res, err := stmt.Exec("rumi", "CSE", "2012-12-09")
checkErr(err)

id, err := res.LastInsertId()
checkErr(err)

fmt.Println(id)

db.Close()

msg := make(map[string]interface{})
msg["body"] = id
return msg
}

我遇到的错误:

➜  functions git:(master) ✗ doctl serverless deploy . --remote-build
Deploying '/home/rumi/go/src/github.com/manjurulhoque/digitalocean-cloud-functions/functions'
  to namespace 'fn-b799454253a-a40440-4639-937f-05102a48c06e'
  on host 'https://fa45as-sgp1-18b45c02afgc.doserverless.co'
Submitted action 'blog/createBlog' for remote building and deployment in runtime go:default (id: b3d5421ee5656bb44c4295421eebb44c642cf)
Submitted action 'sample/hello' for remote building and deployment in runtime go:default (id: b3d5421ee5656bb44c4295421eebb44c642cf)
Submitted action 'blog/db' for remote building and deployment in runtime go:default (id: edcc9eefce9f4aa58c9eefce9f2aa5e6)
Transcript of remote build session for action 'blog/db':
Output of failed build in /tmp/slices/builds/fn-b79956253a-a4080-465639-95637f-05102a48c06e/blog_db/2022-10-22T04-23-08.642Z/packages/blog/db
initializing modules
go: creating new go.mod: module exec
go: to add module requirements and sums:
        go mod tidy
building
db.go:6:2: no required module provides package github.com/mattn/go-sqlite3; to add it:
        go get github.com/mattn/go-sqlite3

Deployed functions ('doctl sbx fn get <funcName> --url' for URL):
  - blog/createBlog
  - sample/hello
Failures:
Error: While deploying action 'blog/db': './build.sh' exited with code 1

project.yml

environment: {}
parameters: {}
packages:
  - name: blog
    environment: {}
    parameters: {}
    annotations: {}
    functions:
      - name: db
        binary: false
        main: ''
        runtime: 'go:default'
        web: true
        parameters: {}
        environment: {}
        annotations: {}
        limits: {}

我没有找到任何好的资源,虽然连接到数据库。任何帮助将不胜感激。

h7wcgrx3

h7wcgrx31#

在DO中,每个函数本身都是一个应用程序,所以你需要在每个函数目录中创建go.modgo.sum
下面是我的一个项目结构

注意:我写了一篇关于如何为DO https://medium.com/@manzurulhoque/use-package-in-digital-ocean-do-serverless-functions-using-golang-cb5200ab22ee设置golang应用程序的文章

相关问题