简体   繁体   中英

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

slacknotification.go contains the following data

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

package main

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

go.mod contains the following data

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在主文件夹中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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