Problem

In this post, I would demo how to solve the problem as follows when I develop a helloworld program with golang:

can't load package: cannot find module providing package bswen.com/goinaction/code/chapter2/test: working directory is not part of a module

Environments

The environments are as follows:

  • go version: go version go1.14.2 darwin/amd64
  • GOROOT: /usr/local/go
  • GOPATH: /Users/me/GoProjects/src
  • IDE: Intellij Idea Ultimate 2019.3 with go plugin

My project is located at $GOPATH/bswen.com/goinaction/code/chatper2/test

The code

The code is as follows:

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}

Run the code and get error

When I run the above the code, I get this error:

can't load package: cannot find module providing package bswen.com/goinaction/code/chapter2/test: working directory is not part of a module

How to solve the problem

I think there maybe a module problem with my project, so I execute this command to init my module:

go mod init bswen.com/goinaction/code/chapter2/test

Then I got this file go.mod in the directory $GOPATH/bswen.com/goinaction/code/chatper2/test, the content is:

module bswen.com/goinaction/code/chapter2/test

go 1.14

Then I rerun my helloworld, I got this:

hello world

Process finished with exit code 0

References

You can view some references as follows: