简体   繁体   English

Go - 无法在同一项目中导入模块

[英]Go - Unable to import module in the same project

I'm unable to import a module within the same project.我无法在同一个项目中导入模块。 My project structure is like something below.我的项目结构如下所示。 Anybody knows what could be wrong here?有人知道这里有什么问题吗?

slacknotifier
|-go.mod
|-main.go
|-slacknotification
  |-slacknotification.go

I'm trying to import slacknotification inside main.go我正在尝试在 main.go 中导入 slacknotification

slacknotification.go contains the following data slacknotification.go 包含以下数据

package slacknotification

import (
    "bytes"
    "encoding/json"
    "errors"
    "net/http"
    "strings"
    "time"
)

//NotifConfig contains data that is then concatenated into one string
type NotifConfig struct {
    message   string
    channelID string
}

//ConcatenateMessageIntoString concatenates data from NotifConfig into one
//string "/n" separated
func ConcatenateMessageIntoString(n NotifConfig) (msg string) {
    values := []string{}
    values = append(values, "New Message Incoming")
    values = append(values, "Message: "+n.message)
    values = append(values, "ChannelID: "+n.channelID)

    msg = strings.Join(values, "\n")
    return msg
}

main.go contains the following data main.go 包含以下数据

package main

import (
    "fmt"
    "log"
    slack "slacknotifier/slacknotification"
)

go.mod contains the following data go.mod 包含以下数据

module slacknotifier

go 1.15

I can see the following error when I check the import details检查导入详细信息时可以看到以下错误

could not import slacknotifier/slacknotification (cannot find package "slacknotifier/slacknotification" in any of 
    /usr/local/go/src/slacknotifier/slacknotification (from $GOROOT)
    /Users/myuser/src/slacknotifier/slacknotification (from $GOPATH))

Is there anything I forgot to configure?有什么我忘记配置了吗?

您需要在slacknotification文件夹内 make go mod init github.com/your-username/slacknotification并添加replace github.com/your-username/slacknotification => ./slacknotificationgo.mod在主文件夹中。

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

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