doulong4169 2013-09-29 02:28
浏览 66
已采纳

导入之前我必须先拥有文件没有任何意义

How do I import an external package from scratch?

I've written a library package in Go and testing to distribute through github. I am following http://golang.org/doc/code.html and using mac but getting error message

cmd I put is following.

$ mkdir $HOME/go
$ export GOPATH=$HOME/go
$ export PATH=$PATH:$GOPATH/bin
$ mkdir -p $GOPATH/src/github.com/user
$ mkdir $GOPATH/src/github.com/user/project

Then I put

$ go get github.com/user/project

Still errors with go install

can't load package: package github.com/golingorg/goling: no Go source files in /Users/user_name/go/src/github.com/user/project

I do not understand why we need files to import an external package in Go. External package means that I get something and create files from the external package.

My question is how I import an external package from scratch. Most of documents just say something like

 go get github.com/yasushi-saito/fifo_queue

this gives me "$GOPATH not set." I am getting frustrated setting up the environment for "go get" to work, as a beginner. Thanks a lot in advance.

Summary

   How do I import an external package from scratch?
  • 写回答

3条回答 默认 最新

  • doubi8512 2013-09-29 04:31
    关注

    Go is a static type language thus it needs to resolve any reference to external package at compile time. The "go" tool expects the source of external packages in locally accessible path thus you need to use "go get" to download them.

    From what you described, you probably did not set the GOPATH. Use ECHO $GOPATH to check if it is set correctly.

    For my GO project, I normally use GOPATH as workspace, similar to virtualenv in Python or rbenv/rvm in Ruby. Let say my project "myproject" has root at /projects/myproject, my source file will be located at /projects/myproject/src/myproject and there is an import of "github.com/user/project", then

    > cd /projects/myproject
    > export GOPATH=`pwd`          # or export GOPATH=/projects/myproject
    > go get github.com/user/project
    

    After "go get" command, the source of "github.com/user/project" will be downloaded to /projects/myproject/src/github.com/user/project.

    When you use "go build" or "go install" then, it will compile as the external packages is in the $GOPATH/src folder.

    If you install Go in the default folder, you need to include Go installed bin folder in the PATH environment variable. After that GOPATH is the other environment variable you need for "go" tool to work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64