登录
首页 >  Golang >  Go问答

将无服务器应用部署到DigitalOcean中的Golang教程

来源:stackoverflow

时间:2024-03-11 18:18:26 277浏览 收藏

各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题《将无服务器应用部署到DigitalOcean中的Golang教程》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!

问题内容

我正在尝试在数字海洋无服务器功能中构建 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  --url' for url):
  - blog/createblog
  - sample/hello
failures:
error: while deploying action 'blog/db': './build.sh' exited with code 1

项目.yml

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

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


正确答案


在 DO 中,每个功能本身就是一个应用程序。所以需要在每个函数目录中创建go.modgo.sum

下面是我的项目结构之一

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

理论要掌握,实操不能落!以上关于《将无服务器应用部署到DigitalOcean中的Golang教程》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>