Importing own modules

Hey,
I know this is a very basic question but I am struggeling working my way through the tutorial Call your code from another module - The Go Programming Language . My directory structure looks like this:

  • Go
    • greetings
    • hello
      in greetings there is the file greetings.go:
package greetings

import "fmt"

// Hello returns a greeting for the named person.
func Hello(name string) string {
    // Return a greeting that embeds the name in a message.
    message := fmt.Sprintf("Hi, %v. Welcome!", name)
    return message
}

and the corresponding go.mod file.
In hello there is the file hello.go:

package main

import (
    "fmt"

    "example.com/greetings"
)

func main() {
    // Get a greeting message and print it.
    message := greetings.Hello("Gladys")
    fmt.Println(message)
}

all according to the tutorial. Yet when I try to compile the module hello I get the following error:

example.com/hello imports
	example.com/greetings: cannot find module providing package example.com/greetings: unrecognized import path "example.com/greetings": reading https://example.com/greetings?go-get=1: 404 Not Found

Can somebody tell me what I have done wrong? Many thanks!

Did you create the mod file?
go mod init example.com
go mod tidy

Thanks for your answer. Yes, I did.

greetings has to be a sub folder of the folder that main is in. main has to be in the same folder as the go.mod file, all this is very well documented in the official documentation.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.