如何从内部文件夹导入Go文件?

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

How to import go file from internal folder?

问题

我有一个Go项目,其结构如下:

- internal
  - client.go
main.go
go.mod

go.mod:

module github.com/zhaoyi0113/eml-transaction

go 1.17

require github.com/go-resty/resty/v2 v2.7.0

require golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect

client.go:

func SendTransaction() {

}

main.go:

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
import "github.com/zhaoyi0113/eml-transaction/internal"

func main() {
  SendTransaction()
}

问题是main.go中的SendTransaction不可见。当我运行go build时,出现以下错误:

./main.go:11:8: imported and not used: "github.com/zhaoyi0113/eml-transaction/internal"
./main.go:36:2: undefined: SendTransaction
./main.go:36:18: undefined: TransactionRequest

我不明白为什么导入没有被使用。导入的正确方式是什么?

英文:

I have a go project whose structure is:

- internal
  - client.go
main.go
go.mod

go.mod:

module github.com/zhaoyi0113/eml-transaction

go 1.17

require github.com/go-resty/resty/v2 v2.7.0

require golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect

client.go:

func SendTransaction() {

}

main.go

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
import "github.com/zhaoyi0113/eml-transaction/internal"
func main() {
  SendTransaction()
}

The problem is that SendTransaction is invisible in main.go. When I run go build, I got below error:

./main.go:11:8: imported and not used: "github.com/zhaoyi0113/eml-transaction/internal"
./main.go:36:2: undefined: SendTransaction
./main.go:36:18: undefined: TransactionRequest

I don't understand why the import is not used. What is the right way to import it?

答案1

得分: 0

刚刚弄明白了它的工作原理。我必须使用internal作为前缀来调用这个方法。

internal.SendTransaction(internal.TransactionRequest{})
英文:

Just figured out how it works. I have to use internal as the prefix to call this method.

internal.SendTransaction(internal.TransactionRequest{})
``

</details>



huangapple
  • 本文由 发表于 2022年3月10日 09:41:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/71417896.html
匿名

发表评论

匿名网友

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

确定