How Call A Package That Is Not In GOROOT

Hello, I am trying to get variables from another package nevertheless I am getting package pkg/variable is not in GOROOT (C:\Program Files\Go\src\pkg\variable). I do not want to create a pkg in goroot, how can I call a package that is not in goroot? My main code is inside Desktop\MyApp and the variable pkg is in Desktop\MyApp\pkg. Below the codes showing how I am trying do it:

Variable package (variables.go):

package variables

var (
	array_001  = []string{"a", "b", "c"}
	array_002  = []string{"1", "2", "3"}
)

The main source (myapp.go):

package main

import (
	"fmt"
	variables "pkg/variable"
)

func main() {

	w := variables.array_001
	fmt.Println(w)
}

You need to create a Go module, then then you can resolve packages from within that module.

I have :
Myapp (folder) >> variables (folder) + go.mod + myapp.go
variables (folder) >> variables.go

go.mod:

module myapp.go

go 1.18

The module word is underlined by red saying : Desktop\MyApp\go.mod:2:2: unknown directive: variables.go

As your module is myapp.go the path to your variables would be myapp.go/pkg (or myapp.go/pkg/bariables, the folder layout is not exactly clear in what you write).

And I do not care for squiggly lines. As long as it compiles on the terminal everything is fine, and if there is an error in the terminal, tell us that.

1 Like

Perfect, that worked. Thank you so much @NobbZ