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 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗