简体   繁体   English

在 Golang 中找不到导入的包

[英]Cannot find imported package in Golang

I am trying to learn Golang, and I have a beginners issue.我正在尝试学习 Golang,但我有一个初学者问题。

I created two files "helloworld.go" and "greetings.go".我创建了两个文件“helloworld.go”和“greetings.go”。 I am trying to import greetings.go into helloworld.go using我正在尝试将 greetings.go 导入 helloworld.go 使用

import (
    "test/greetings/greetings"
)

But I get the following error:但我收到以下错误:

cannot find package "test/greetings/greetings" in any of: C:\\Go\\src\\test\\greetings\\greetings (from $GOROOT) C:\\work\\go-ws\\src\\test\\greetings\\greetings (from $GOPATH) exit status 1在以下任何一个中找不到包“test/greetings/greetings”:C:\\Go\\src\\test\\greetings\\greetings(来自 $GOROOT) C:\\work\\go-ws\\src\\test\\greetings\\greetings(来自 $ GOPATH) 退出状态 1

I am using a windows OS and my GOPATH and GOROOT values are as follows:我使用的是 Windows 操作系统,我的 GOPATH 和 GOROOT 值如下:

"GOPATH": "C:/work/go-ws"
"GOROOT": "C:/Go"

My project structure is as shown:我的项目结构如图:

C:
--work
   -- go-ws
      -- anlytics
          -- src
              -- test
                  -- greetings
                       **greetings.go**
                  **helloworld.go**
          -- pkg
          -- bin

Where am I going wrong?我哪里错了?

To use the 'Salutation' struct from the greetings.go, I had to append the greet package to the variable.要使用 greetings.go 中的“Salutation”结构,我必须将 greet 包附加到变量中。 Instead of 'Salutation', you use it with greet.Salutation.您可以将它与greet.Salutation 一起使用,而不是“Salutation”。

My greetigs.go file我的greetigs.go文件

package greet

import "fmt"

type Salutation struct {
    Name     string
    Greeting string
}
...

So to use it in my helloworld.go, I append greet to the variable as follows所以要在我的 helloworld.go 中使用它,我将 greet 附加到变量中,如下所示

package main

import (
    "test/greetings"
)

func main() {

    var s = greet.Salutation{"Bob", "Hello"}
}

And it works...它有效...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM