从另一个目录导入文件

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

Go import file from another directory

问题

我正在尝试将utility导入到github_events.go中。

utility.go文件位于services目录下。

utility.go的内容如下:

package utility

import (
	"os"
    "regexp"
)

我的项目结构如下:

从另一个目录导入文件

这是从github_events.go中导入的方式:

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"
"sort"

"github-app/services/utility"
)

我还尝试使用别名utility "github-app/services/utility"

但是我得到了以下错误could not import github-app/services/utility (no required module provides package "github-app/services/utility")compilerBrokenImport

我的go.mod文件内容如下:

module github-app
go 1.18

我做错了什么?

英文:

I'm trying to import utility into github_events.go.

utility.go is placed under services directory.

utility.go looks like this:

package utility

import (
	"os"
    "regexp"
)

My project structure looks like this:

从另一个目录导入文件

This is how import from github_events.go

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"
"sort"

"github-app/services/utility"
)

I also tried with an alias utility "github-app/services/utility"

But I get the following error could not import github-app/services/utility (no required module provides package "github-app/services/utility")compilerBrokenImport

My go.mod file:

    module github-app
    go 1.18

What am I doing wrong?

答案1

得分: 2

只需导入"github-app/services",你就可以开始了。

英文:

Just import "github-app/services" and you're good to go.

huangapple
  • 本文由 发表于 2023年1月19日 17:22:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75170024.html
匿名

发表评论

匿名网友

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

确定