Thursday, September 15, 2022

Golang Cheat Sheet

Golang is a statically typed and compiled programming language designed at Google. Go plays a major role in Enterprises by developing highly scalable concurrent cloud computing systems like Docker + Kubernetes.

Let's check it out!

Installation
Download and install Go as per instructions for your target operating system. Afterwards verify go version.

Windows
Download and open the Go MSI file. Follow all prompts and ensure Go added to PATH environment variable.

Mac OS/X
Download and open the Go PKG file. Follow all prompts and ensure Go added to PATH environment variable.

Linux
Download and extract the Go archive. Launch Terminal and ensure Go added to PATH environment variable.
 sudo rm -rf /usr/local/go
 sudo tar -C /usr/local -xzf go1.18.3.linux-amd64.tar.gz
 export PATH=$PATH:/usr/local/go/bin/
 go version
Alternatively, include export PATH="$PATH:/usr/local/go/bin/" to your ~/.bashrc file for future use.

IMPORTANT
If Go does not install or update then you may have to remove snap package using sudo snap remove go. Also, verify Go environment variables by entering go env at the Terminal prompt for all operating systems.

Uninstall
Follow this guide on how to uninstall Go. While there seems to be many options, the following should work:
 sudo apt-get remove golang-go
 sudo apt-get remove --auto-remove golang-go

IDEs
Now we'd like to build Golang software so let's setup 2x main Integrated Development Environments IDEs:

VS Code
Visual Studio Code is a popular lightweight IDE available cross platform on Windows, Mac OS/X and Linux. Install VS Code and install the following plugins: Go, Go Doc, Go Outliner, Go Autotest + Go Test Explorer.
 Go  Rich Go language support for Visual Studio Code
 Go Doc  Show Go's documentation symbols and packages
 Go Outliner  Go code outline explorer and navigation package
 Go Autotest  Adds autotest functionality to vscode's Go mode
 Go Test Explorer  Go Test Explorer for unit and integration testing

Launch VS Code. Press Ctrl + Shift + P. Filter on "Go: Install". Choose "Go: Install/Update Tools". Select all. Ok. Also, if prompted "The "gopls" command is not available." bottom right then click Install to go get gopls.

Miscellaneous
Launch Terminal. Drag tab headings to prefer following order: Terminal, Problems, Output, Debug Console.

Mappings
If you are transitioning from Windows to Linux you may like to update keyboard mappings for example Back to Alt + LeftArrow. File | Preferences | Keyboard Shortcuts. Filter "Go Back". Click pencil. Hit Alt + LeftArrow.

Repeat process for Terminal: Copy Selection: File | Preferences | Keyboard Shortcuts. Hit Ctrl + C and Enter. Repeat process for Terminal: Paste into Active Terminal: File | Preferences | Keyboard Shortcuts. Hit Ctrl + V.

Navigation
If you are transitioning from Visual Studio on Windows to VS Code then the Solution Explorer with folders being represented as folder icons not arrows may be more familiar. Therefore, install vscode-icons plugin. Also, as code bases become larger it can be handier to install the Bookmarks plugin for easier navigation.

Shortcuts
 Ctrl + ,  Open Settings similar to Preferences  F5  Debug | Start w/ Debugging
 Ctrl + P  Search files to open e.g. main.go  Ctrl + F5  Debug | Run w/o Debugger
 Ctrl + R  Select to open from previous files  F9  Debug | Toggle Breakpoint
 Ctrl + Shift + O  Outline file structure for opened file  F10  Debug | Step Over
 F12  Navigate | Go to Definition  F11  Debug | Step Into
 Alt + Left  Navigate | Going Backward  Shift + F11  Debug | Step Out

Zoom In
Finally, you may like to Ctrl + "+" to zoom in on the Explorer and Terminal then reduce the Font Size to 12.


GoLand
GoLand is a cross-platform Go Integrated Development Environment IDE on Windows, Mac OS/X and Linux. Install GoLand and setup the following configurations as commercial alternative with better IDE Intellisense.

Configurations
Launch GoLand. In the "Welcome to GoLand" popup choose Customize | Color theme | IntelliJ Light. Next, choose All settings... Expand Go tab at the top | GOROOT | Add SDK... Local | Navigate to where Go was installed previously e.g. /usr/local/go. Finally update GOPATH. Click "+" select ~/go. OK | Apply | OK.


IMPORTANT
Verify GOROOT path is correct from Terminal via go env | grep GOROOT. Repeat for go env | grep GOPATH. Also, in Go tab | Go Modules ensure "Enable Go modules integration" is checked as may not be by default.


Mappings
Align GoLand Keymap to VS Code Keymap for consistent keyboard shortcuts and developer IDE experience. In the "Welcome to GoLand" popup choose Plugins | VSCode Keymap | Install. Navigate back to Customize. Under Keymap change the default value to VSCode. Now all VS Code shortcuts above will work in GoLand.

Additionally to Ctrl + Shift + O, outline file structure for opened file, GoLand has these handy shortcuts also:
 Ctrl + Numpad -  Collapse struct types for easier overall type visibility in IDE
 Ctrl + Numpad +  Expand struct types to navigate type properties + methods

NOTE
If Ctrl + Shift + O launches Symbols then remove duplicate: Choose Settings | Keymap | filter as "symbol". Double click Navigate | Go to Symbol | Edit | Remove Ctrl+Shift+O | Apply | OK. Repeat this as necessary.

Settings
Finally, from Customize | All settings... here are some more general GoLand IDE settings that can be useful:
 Editor | General | Code Folding | Fold by default | General  UNCHEKED
 Editor | Font  JetBrains Mono | Size: 14.0 | Line: 1.2
 Editor | Color Scheme Font  Scheme: Classic Light
 Editor | Inspections | Proofreading | Typo  UNCHEKED

Example
Create the obligatory Hello World program in both VS Code and GoLand IDE to complete the configration:

main.go
package main
import "fmt"

func main() {
	fmt.Println("Hello World!")
}

VS Code
Create "HelloVSCode" folder at ~/go/src. Launch VS Code. Open folder [Ctrl+K, Ctrl+O] HelloVSCode folder. New File | main.go. Enter + save Hello World source code from package main above. Launch new Terminal:
go mod init HelloVSCode
go mod tidy

go build .
go run .
Set breakpoint in main.go. Run menu and choose either Start Debugging or Run without Debugging option.


GoLand
Launch GoLand. New Project | Enter Location and GOROOT as above i.e. Go 1.18.3 /usr/local/go. Create.

 Location  ~/go/src/HelloGoLand
 GOROOT  Go 1.18.3 /usr/local/go
 Enable vendoring support automatically  CHECKED

In main.go right click triangle next to func main() | Modify Run configuration... Ensure there are no errors. If there are errors then ensure if Run kind is Package the package name matches in go.mod. Otherwise choose File | main.go. Once confirmed no errors then right click triangle and choose the Run or Debug configuration.

Cache
If GoLand complains "cannot resolve symbol" then choose File | Invalidate Caches... | Invalidate and Restart.

Configurations
Track active item in solution explorer similar to Visual Studio: Click cog to right of Project and Always Select Opened File. Hit shift key twice for quick search. Remember Rename files and folder is in the Refactor menu!

Layout
It may be good idea to position Problems tab next to code window so you can see issues as you enter code. In GoLand, click cog on Problems | Move to | Right Top. In VS Code right click Problems | Move Panel Right.

Navigation
As code bases become larger use the Bookmarks feature for easier navigation. Right click the code gutter + Add Bookmark. View menu | Tool Windows | Bookmarks. This can also be used to edit/remove Breakpoints.

Source Control
In GoLand, by default the Staging area may be disabled. Therefore, you wouldn't see any Unversioned files To disable changelists choose File menu | Settings | Version Control | Git | Enable staging area | UNCHECK.

In GoLand, top right under Project is the Control tab. Right click any file and choose Rollback to revert any changes. In VS Code the Source Control tab is third tab. Choose Discard Changes as corresponding action.


Summary
To summarize, we have a simple setup for Golang programming on Windows, Mac OS/X and Linux. There is much to explore e.g. Cloud Computing and developing highly scalable concurrent applications using Golang!
This will be topic of the next post.