doukekui0914 2016-05-10 19:21
浏览 200
已采纳

Golang不会重新编译某些文件

I just got an error:

server

server/server.go:73: undefined: foo.MyName

I didn't understand at first and I tried to change the name of my function etc.. So I created a function called MyName() which return a string which is my name..

But, the error just came up again.. After more than one hour, I just tried to comment all of my code in the package foo ! I wanted to say f**k that at this moment... #PS: I also comment the server.go:73 line.. and.. It compiles and totally works?!

I commented my code, but it seems to be still here?! So now my question is:

Does Golang have some problem to recompile some file?

-------------------------------------

Workspace

pkg

    linux_amd64

src

   algo  database_sql  github.com  golang.org  GoogleAPI  googlemaps.github.io  main  main.go  models  myDebug  router  server

I'm making "go build main.go"

I also read How to Write Go Code without find any solution...

Some hints? Thank !

  • 写回答

1条回答 默认 最新

  • douduan9129 2016-05-10 21:29
    关注

    So I'm guessing that your dev tree looks something like this:

    src
    ├───algo
    ├───database_sql
    ├───github.com
    ├───golang.org
    ├───GoogleAPI
    ├───googlemaps.github.io
    ├───main
    ├───main.go
    ├───models
    ├───myDebug
    ├───router
    └───server
        └───server.go
    

    As you can see in How to Write Go Code, you are supposed to have a folder for your project, so main.go should be someproject/main.go and you can then just use go build to build the entire project.

    Ideally you will want to have all your projects in a folder for their web address, like github.com/yourname/projectname. I recommend you follow this convention even if a specific project is not going to be publicly accessible. I keep all my projects (even the ones that are not in github) in my github/my user folder. This way it's easier to find and classify all you software.

    If you want to build a project and all of it's dependencies (in this case server/server.go) you can do go build ./...

    You can also go into the server package and do go install to install build and reinstall the package.

    So you should have something like this:

    GOPATH
    ├───bin
    |   └───main (this will have the name of the folder)
    ├───pkg (this routes match the ones on src)
    |   └───linux_amd64
    |       └───server.a
    └───src
        ├───algo
        ├───database_sql
        ├───github.com
        ├───golang.org
        ├───GoogleAPI
        ├───googlemaps.github.io
        ├───main
        |   └───main.go
        ├───models
        ├───myDebug
        ├───router
        └───server
            └───server.go
    

    And then go to src/main/ and do go build ./... to build all the dependencies. Ideally you will want to do go install ./... so that libraries are installed on the correct path, and your binaries also get created in the correct path and not in the src folder. If you do this, you will need to run the binary bin/main. I recommend that you add GOPATH/bin to your path so that you can install your software and just run it without going to the bin folder.

    Probably there is some error in the way you are importing or using the server package. This is the best I can do without you posting your code.

    Edit: After further thinking about this, I believe I found the error. You were doing go build to build the whole project, and at some point you did go install on the server library. So now go build is using the already compiled version of the library in pkg, so if you want to fix the problem do go install on the server file or go install ./... on the main folder.

    This is not an error in Go, usually you don't want a change in a library to break your code, so you have to remember to install the new version of a library before trying your code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记