druzuz321103 2017-03-26 19:46 采纳率: 100%
浏览 78
已采纳

Go和Gogland的一些提示

Hi all. I'm very new with Go and Gogland. I have a project Go project in Gogland

  1. I choose "Run kind" as Package - to run not only main file but a project. Why it cannot find main package??
  2. How to import util.myprinter package to main.go to use it??

Please, help me

  • 写回答

1条回答 默认 最新

  • douzong0711 2017-03-26 20:16
    关注

    First, the general structure of your Go workspace seems to be wrong. You need to make it look more like this:

    D:
    |-- go_projects
    |    |-- bin
    |    |-- pkg
    |    |-- src 
    |    |    |-- FirstSteps
    |    |    |    |-- main.go
    |    |    |    +-- util
    |    |    |         +-- myprinter.go
    |    |    |-- SecondProject
    |    |    |-- ThirdProject
    ...
    

    Second your import statement seems to be empty, I have no idea how goglang works but if you want to use whatever is in your myprinter.go file, you will need to import the util package, assuming that the myprinter.go file declares its package as util at the top.

    // FirstSteps/main.go
    package main
    
    import (
        "FirstSteps/util"
    )
    
    func main() {
        util.MyPrinterFunc()
    }
    

    And of course to be able to use anything from util there first must be something...

    // FirstSteps/util/myprinter.go
    package util
    
    func MyPrinterFunc() {
        // do stuff...
    }
    

    Edit: I'm sorry, I didn't actually answer your question initially. You're getting the error Cannot find package 'main' because of the wrong workspace setup I already mentioned. The Package path tells Gogland where the package you want to run is relative to the $GOPATH/src directory. So after you've setup your wrokspace correctly, you should set the Package path to FirstSteps since that package's absolute path will be $GOPATH/src/FirstSteps. If, later, you want to run the util package you would specify Package path as FirstSteps/util for gogland to be able to find it.

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

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据