How do packages with multiple files work in golang?

Issue

This repo has 3 go files all begin with “package lumber”.
To use this package, I’m supposed to put this in my GOROOT and simply

import lumber

in my program. How do variables and types in this package connect with each other across multiple files? How does the go compiler know which file to begin reading first?

In case I want to read the package, where should I begin reading to understand the package? What exactly is the flow of things here?

Solution

No, you’re not “supposed to put this in my GOROOT”. You’re supposed to execute

$ go get github.com/jcelliott/lumber

which will clone the repository into $GOPATH/src/github.com/jcelliott/lumber. Then you can use the package by importing it in your code as

import "github.com/jcelliott/lumber"

About the scoping rules: Declarations and scope

Answered By – zzzz

Answer Checked By – Pedro (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.