[newbie] packaging and including.

xuanbao · 2017-10-07 00:00:05 · 636 次点击    
这是一个分享于 2017-10-07 00:00:05 的资源,其中的信息可能已经有所发展或是发生改变。

I am following this guide.

So at my current working directory I have five files. handles.go, logger.go, routes.go routes.go and main.go.

My main.go looks like the following:

package main

import (
    "log"
    "net/http"
)

func main() {

    router := NewRouter()

    log.Fatal(http.ListenAndServe(":8080", router))
}

NewRouter is a function defined in routes.go. Several other functions are also being called among those .go. In the tutorial, I see no include/import statement for those .go files; My guess is there must be some parameters that's being passed in go build or go run that will incorporate all the files? if so what is it?


评论:

sxan:

If all files are in the same directory, and they're all in package main, no imports are needed.

juniorsysadmin1:

it doesn't work when I do go run main.go it complain about the routerfunction that is defined in routes.go.

TheMerovius:

Use go build or go run *.go. run and build have very different behaviors, run is for running a set of go-files, whereas build is for building a package.

bishopknight1977:

If you're using Gogland for IntelliJ you need to specify the run configuration to package . I had the same problem until I changed it.

thornag:

You need to include all the files when running or building

go run ./... or go run *.go


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

636 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传