dpafea04148 2019-06-04 20:03
浏览 67

如何使用Now(zeit.co)在golang中使用本地文件?

I'm creating an api with golang and zeit-now. I want the API to return a password generated from a list of words (30k). Everything works if I use a local server (http.ListenAndServe), but when I try to access the concerned endpoint with the now link, an error occurs.

I tried to use the config.includeFiles option, but it doesn't work. I also tried to use local path, absolute path but nothing is working.

Here is the now.json and randDict.go files:

now.json

"builds": [
            {
                "src": "/lambdas/randDict/randDict.go",
                "use": "@now/go",
                "config": {
                    "includeFiles": [
                        "template/wordsGist"
                    ]
                }
            }
]

randDict.go

// Create a slice with the words from the words' file
func createWordsSlice() ([]string, int, error) {
    // Read all the words inside the file "wordsGist"
    file, err := ioutil.ReadFile("template/wordsGist")
    if err != nil {
        fmt.Println(err)
        return nil, 0, err
    }
    // Split the words into a slice
    words := strings.Split(string(file), "
")
    return words, len(words), nil

}

Here I expect a slice with all the words, which I'll be using later for password generation, but I get an error and a empty slice (nil).

Here is the github if you want to read all the code.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目
    • ¥20 mysql架构,按照姓名分表
    • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题