【发布时间】:2022-06-11 00:17:36
【问题描述】:
我在尝试使用内部包时遇到问题。
这是我的项目结构:
.
├── go.mod
├── main.go
└── services
└── business.go
services/business.go 是:
package services
import (
"math"
)
type Circle struct {
Radius float64
}
func (c Circle) Area() float64 {
return math.Pi * math.Pow(c.Radius, 2)
}
type Square struct {
Width float64
Height float64
}
func (s Square) Area() float64 {
return s.Width * s.Height
}
type Sizer interface {
Area() float64
}
我正在尝试使用 main.go 中的服务包:
package main
import "fmt"
import "./services"
func main() {
fmt.Printf("Hello World, %s.\n", "Jordi")
c := Circle{Radius: 10}
s := Square{Height: 10, Width: 5}
l := Less(c, s)
fmt.Printf("%+v is the smallest\n", l)
}
func Less(s1, s2 Sizer) Sizer {
if s1.Area() < s2.Area() {
return s1
}
return s2
}
目前,我得到:
无法导入服务(不需要模块提供包“服务”)
之后,我尝试执行:go get ./services,但问题一直失败。
有什么想法吗?
编辑:
我的模块是:
module me/jeusdi/goplay
go 1.18
我试过了:
import "me/jeusdi/goplay/services"
不过,我现在收到了这条消息:
“me/jeusdi/goplay/services”已导入但未用作服务
【问题讨论】:
-
“有什么想法吗?” -- 不要使用相对导入。使用完整的导入路径。见:go.dev/doc/code#ImportingLocal
-
你试过不带斜线吗?
-
我试过不带斜线。目前,只有
"me/jeusdi/goplay/services" imported but not used as services保留。有什么想法吗?
标签: go