Working with files without mod

I’ve 2 files in the same folder:

// file path and name: go_first_steps/main.go
package main

import "fmt"

func main() {
	fmt.Println(a)
	fmt.Println(b)
}

// file path and name: go_first_steps/constants.go
package main 

const (
a = `Hello`
b = `Gopher`
)

And upon running go run main.go I got the below error:

# command-line-arguments
.\main.go:8:14: undefined: a
.\main.go:9:14: undefined: b

you must compile all the files. In another word:

$ go run main.go constants.go

Alternatively, you can use directory location as the target:

$ go run .  # refers to current directory

Reason: compiler understands you want to run main.go but does not understand you want immediate dependence across neighbor files.

1 Like

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