Import a local subpackage into main.go

Hi, everyone.I am a beginner in golang.I am feeling so confused about module management and i get error when i try to import local package and use the parament. It’s just so weird!

My golang env setting is as follows

GO111MODULE="on"
GOPATH="/root/go"
GOROOT="/usr/local/go"
GOVERSION="go1.18.2"

My folder looks like this:

test-module
--dir1
----dir1.go
--go.mod      
--main.go     

The file main.go import the dir1 package and print the parament of a.

main.go

  1 package main
  2 import(
  3     "fmt"
  4     "test-module/dir1"
  5 )
  6
  7 func main(){
  8     fmt.Println(dir1.a)
  9 }

go.mod

module test-module

go 1.18

dir1.go

package dir1

var a = 2

And when i try to run the main file. (go run main.go), an error says.

# command-line-arguments
./main.go:8:19: undefined: dir1.a

lowercase symbols do not get “exported” from a package. You need to use an uppercase A.

1 Like

Thx!!!!!!!!!!! :smiling_face_with_three_hearts:

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