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 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示