2014-09-29 08:20



I just started with Go, and i love it! I have tried to make the structure of my project a bit more manageable, instead of having everything in my main.go

So now i have a structure like this.

-> main.go
-> routes.go
-> handlers/
--> user_handlers.go

But when i try to build this with the following command

go build -v -o ./bin/my_bin ./src/...

I get this error

cannot use -o with multiple packages

But if i make it a flat structure like this

-> main.go
-> routes.go
-> user_handlers.go

It works just fine, all my files got "package main" in the top of them.

What am i doing wrong?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dsarttv037029 dsarttv037029 5年前

    The package name must match the directory name. Moving a source file to a new directory requires that you also change the package name.

    foo/foo.go // package foo
    foo/bar/bar.go // package bar
    foo/bar/qux.go // package bar

    The PATH is not relevant in terms of the package name.

    Package foo: /some/path/some/where/foo 

    This allows multiple "foo" packages to be created and imported provided your import specifies the desired location of "foo"

    P.S. The convention for package names is lowercase, no punctuation (e.g., no _'s)

    点赞 评论 复制链接分享
  • doudeng2184 doudeng2184 7年前

    It tells you what you did wrong, you can't separate a single package over multiple folders.

    You need to set and properly use $GOPATH and properly import your routes/ folder in routes.go.

    A simple example of it is:

    // routes.go
    // the . means you can call imported functions without prefixing them with the package name
    import . "full-path-to-routes/-relative-to-$GOPATH"


    The GOPATH environment variable specifies the location of your workspace. It is likely the only environment variable you'll need to set when developing Go code.

    To get started, create a workspace directory and set GOPATH accordingly. Your workspace can be located wherever you like, but we'll use $HOME/go in this document. Note that this must not be the same path as your Go installation.

    I highly recommend reading Effective Go.

    点赞 评论 复制链接分享