去定制包导入错误

时间:2016-04-05 17:55:03

标签: go

我正在尝试编写一个程序,我的结构如下:

Go/src/
  -github.com
     -myname
        -hello
           main.go
           -vector
              vector.go

当我使用如下命令在main.go文件中的代码中导入包时:

import(
  "vector"
)

我收到错误消息:

Can't find package "vector" in any of:
    C:\Go\src\vendor\vector (vendor tree)
    C:\Go\src\vertex (from $GOROOT)
    C:\Go\src\github.com\myname\src\vertex (from $GOPATH)

为什么在最后一行添加src?它不应该用hello文件夹替换src,因为那是我运行文件的地方吗?此外,如果我从像github.com/myname/hello/vertex这样的完整文件结构中导入它,它就会运行。这对我来说很奇怪。

我正在使用go run hello.go执行以简化与程序的交互。

1 个答案:

答案 0 :(得分:3)

你的Go代码(与Go的stdlib相对)意味着在 //Licence verification: var accessXML = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("AccessRequest", new XAttribute(XNamespace.Xml + "lang", "en"), new XElement("AccessLicenseNumber", "LICENCE"), new XElement("UserId", "USERID"), new XElement("Password", "PASSWORD"))); //Shipping request: var serviceXML = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("RatingServiceSelectionRequest", new XAttribute(XNamespace.Xml + "lang", "en"), new XElement("Request", new XElement("TransactionReference", new XElement("CustomerContext", "Rating and Service"), new XElement("XpciVersion", "1.0001")), new XElement("RequestAction", "Rate"), new XElement("RequestOption", "Rate")), new XElement("PickupType", new XElement("Code", "01"), new XElement("Description", "Daily Pickup")), new XElement("Shipment", new XElement("Shipper", new XElement("Address", new XElement("PostalCode", "33706"), new XElement("CountryCode", "US"))), new XElement("ShipTo", new XElement("Address", new XElement("PostalCode", "34221"), //GET from postObject new XElement("CountryCode", "US"))), //GET from postObject new XElement("ShipFrom", new XElement("Address", new XElement("PostalCode", "33706"), new XElement("CountryCode", "US"))), new XElement("Service", new XElement("Code", "03")), //GET from postObject new XElement("Package", new XElement("PackagingType", new XElement("Code", "02")), new XElement("Dimensions", new XElement("UnitOfMeasurement", new XElement("Code", "IN")), new XElement("Length", "20"), new XElement("Width", "20"), new XElement("Height", "20")), new XElement("PackageWeight", new XElement("UnitOfMeasurement", new XElement("Code", "LBS")), new XElement("Weight", "10")))))); //GET from postObject //Combine the strings and POST var requestXml = accessXML.ToString() + serviceXML.ToString(); 下(编辑:不是 $GOPATH/src,正如我最初所说的那样!),它是标准的总是使用完整的导入路径,在你的情况下从$GOROOT开始(即使你找到了避免必须的方法)。

Go团队在How to Write Go Code中有更多内容,此处的其他答案描述了project layoutthe first steps to setting a workspace up