Configuration

Night mode

Global vs. Local Scope

A$AP Learn Go (GoLang) 🚀 Course

Engineers From These Top Companies and Universities Trust EXLskills

1M+ Professionals | 100+ Institutions

This is the EXLskills free and open-source A$AP Learn GoLang Course! It's a highly-accelerated open course that's best-suited for people with a bit of background in software engineering to quickly pick up Go, learn the essential best practices, and hit the ground running!
After this course, you'll be able to build basic Go applications in addition to lightweight webservers, highly-concurrent programs, and reusable libraries in Go that you can share with other developers!
For further practice, we recommend checking out our Go Guided Projects that will give you access to a professional Go developer, detailed documentation, and real-world tasks that you can work on to go from the basics of Go, into building production apps.

Is this course FREE?

Yes, this a 100% free course that you can contribute to on GitHub here!

Have more questions?

Beyond the Basics

Global vs. Local Scope

Public, private, in Go?

Unlike C++ or Java, Go doesn't have access keywords, such as public, private, etc. However, we still have a way to manage access to fields, types, and functions -- capitalization!

Which scopes are we managing access among?

Go allows us to manage access within a package and between packages. For instance, you can make a field only accessible within its own package, or you can allow any package to access it. These rules are also applied during runtime 'reflection' that many libraries you use will be performing, so watch out for the potential affects of your access management practices!

Capitalization matters in Go

The first thing to

package demo
// This will be available to packages other than the current one via import since it starts with a capital character
const DemoConst = 21 // Basically like `public final int DemoConst = 1` for those familiar with Java
// Since this const starts with a lower-case character, it'll only be visible within this package
const constForOurselves = 42;
// PublicStruct will be available to other packages that import this package since it starts with a capital character
type PublicStruct struct {
// We still need to export public fields (this is important for things like JSON decoding/encoding)
FieldWeWantPeopleToSee string
// Even though the struct is exported, we can still have package-only fields
// This field will not be decoded/encoded in JSON because the encoding/json package won't be able to see it
fieldWeDontWantPeopleToSee string
}
// This is an unexported struct because it starts with a lower-case character
type demoStruct struct {
// We can still have exported fields (this is important for things like JSON decoding/encoding)
FieldWeWantPeopleToSee string
// This field will not be decoded/encoded in JSON because the encoding/json package won't be able to see it
fieldWeDontWantPeopleToSee string
}

Keep it Idiomatic!

In Go we:

Do our best to document exported types, constants, variables, and functions -- it's good practice

Generally try to export as few fields as possible

Limitations of Go Access Control

The primary limitation that developers from Java-like languages will notice is that you can only manage access by package -- not by class (or file, since Go doesn't have classes -- just structs). In reality, this tends to be a non-issue, but it is something to keep in mind for those who are used to having more granular control!