登录
首页 >  Golang >  Go问答

Netlify 函数 (AWS Lambda) 示例“hello”Golang 函数:无效或意外的令牌

来源:stackoverflow

时间:2024-04-09 20:57:31 437浏览 收藏

学习Golang要努力,但是不要急!今天的这篇文章《Netlify 函数 (AWS Lambda) 示例“hello”Golang 函数:无效或意外的令牌》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!

问题内容

我正在尝试让 ne​​tlify functions 与 go 一起使用。 首先,我尝试克隆官方示例存储库 (https://github.com/netlify/aws-lambda-go-example),并且成功了。

我的问题是,我有一个 hugo 网站,需要 hugo 构建命令,但我不知道如何使用 hugo 构建 hugo 和使用 make build 的 go 源文件(如示例存储库中所示) - 我认为它可以解决问题,但我找不到描述此选项的相关文档。

所以我的下一步是手动编译go函数文件并将其放入functions文件夹中。

源文件(来自上面的示例):

package main

import (
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func handler(request events.apigatewayproxyrequest) (events.apigatewayproxyresponse, error) {
    return events.apigatewayproxyresponse{
        statuscode: 200,
        body:       "hello aws lambda and netlify",
    }, nil
}

func main() {
    // make the handler available for remote procedure call by aws lambda
    lambda.start(handler)
}

我使用了 https://github.com/aws/aws-lambda-go#building-your-function 上提供的指令来编译 go 二进制文件:

goos=linux goarch=amd64 go build -o hello hello.go
zip hello.zip hello
mv hello.zip ./functions/hello.zip

它被推送到 git,因此部署到 netlify。到目前为止一切顺利,我的函数出现在 netlify ui 中。

但是当我请求函数 url 时,我收到错误消息:

{
  "errormessage": "invalid or unexpected token",
  "errortype": "syntaxerror",
  "stacktrace": [
    "",
    "syntaxerror: invalid or unexpected token",
    "createscript (vm.js:80:10)",
    "object.runinthiscontext (vm.js:139:10)",
    "module._compile (module.js:616:28)",
    "object.module._extensions..js (module.js:663:10)",
    "module.load (module.js:565:32)",
    "trymoduleload (module.js:505:12)",
    "function.module._load (module.js:497:3)",
    "module.require (module.js:596:17)",
    "require (internal/module.js:11:18)"
  ]
}

这是 netlify 的函数日志:

1:18:16 AM: hello invoked
1:18:17 AM: Syntax error in module 'hello': SyntaxError
(function (exports, require, module, __filename, __dirname) { ELF
                                                              ^
SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
1:19:02 AM: hello invoked
1:19:03 AM: Syntax error in module 'hello': SyntaxError
                                                              ^
SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)

此外,netlify ui 中的函数名称似乎是 hello.js - 我不知道是否应该是这样。在我看来,aws 认为它是 javascript,而不是 go。


解决方案


我还没有在 netlify 上测试过压缩的 go 函数。

如果您不想在这种情况下进行手动构建,您可以在 netlify 部署上内联构建命令。

添加一个构建命令,为项目执行这两种构建。

[build]
  command = "make build && hugo"
  functions = "functions"
  publish = "public"
[build.environment]
  # Change this path with the path to your repository
  GO_IMPORT_PATH = "github.com/netlify/aws-lambda-go-example"

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Netlify 函数 (AWS Lambda) 示例“hello”Golang 函数:无效或意外的令牌》文章吧,也可关注golang学习网公众号了解相关技术文章。

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