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.

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?