如何导入本地包?

huangapple go评论159阅读模式
英文:

how to import local packages?

问题

我的GOPATH和GOROOT

GOPATH="/Users/road/IdeaProjects/MiniJVM"
GOROOT="/usr/local/go"

我的golang项目结构

Myproject
---.idea
---src
   ---cmd
      ---cmd.go
   ---test
      ---test.go

test.go文件,我将使用其他包的导入。我的代码有什么问题?或者导入路径有问题吗?

package main

import (
     "fmt"
     "cmd"
)
func main()  {
     command := &Cmd{}//未解析的类型'Cmd'
}

cmd.go文件

package cmd

import (
	"flag"
	"fmt"
	"os"
)

/*
	jaca [-option] class [args...]
*/
type Cmd struct {
	HelpFlag bool
	VersionFlag bool
	CpOption string
	Class string
	Args []string
}
英文:

My GOPATH and GOROOT

GOPATH="/Users/road/IdeaProjects/MiniJVM"
GOROOT="/usr/local/go"

My golang project structure

Myproject
---.idea
---src
   ---cmd
      ---cmd.go
   ---test
      ---test.go

test.go file, I will use the imports from other packages .What is wrong with my code? Or the import path has some problem?

package main

import (
     "fmt"
     "cmd"
)
func main()  {
     command := &Cmd{}//unresolved type 'Cmd'
}

cmd.go file

package cmd

import (
	"flag"
	"fmt"
	"os"
)

/*
	jaca [-option] class [args...]
*/
type Cmd struct {
	HelpFlag bool
	VersionFlag bool
	CpOption string
	Class string
	Args []string
}

答案1

得分: 1

使用完整路径:

import (
    "github.com/myname/myproject/src/cmd"
)
英文:

Use the full path:

import (
    "github.com/myname/myproject/src/cmd"
)

答案2

得分: 1

你可以将文件夹名称"cmd"更改为其他名称,因为标准库中已经有一个包名为"cmd"的包。

英文:

You may change the folder name "cmd" to other name,because there is already an package "cmd" in the standard library.

huangapple
  • 本文由 发表于 2017年9月4日 17:07:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/46033761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定