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 请问下这个红框里面是什么文档或者记事本编辑器
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179