3

I am getting an issue running a function from a non-main package in main.go

// main.go
package main

import test "./tests"

func main() {
    test.Test("hello world")
}


// (relative to main.go) ./tests/test.go
package test

import "fmt"

func Test(str string) {
    fmt.Println(str)
}

Output: build command-line-arguments: cannot find module for path _/c_/Users/Mike/Desktop/random/tests

1
  • 2
    Do not use relative imports in Go. Commented Aug 31, 2021 at 19:44

1 Answer 1

9

If you are using Go 1.16+ use Go modules:

  1. Exec go mod init projectname
  2. Replace import test "./tests" by import test "projectname/tests"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.