My Go language Programming Journey, Kickoff

 Part 1: Getting Started

1.1     Obtaining and installing the Go lang SDK

For Windows OS, open a web browser and go to http:// https://go.dev/dl/

Download the .msi installer package

Execute the downloaded .msi installer

Optional: You may change the default installation folder


Verify the installation: by executing on Cmd shell/terminal:

Ø go version

Ø go env GOROOT




Note that: The golang sdk installer has added the path of its “bin” folder which contains all its executable commands (including “go.exe”) to the Path environment variable.

 

1.2     Setup Go lang programming environment with Visual Studio Code

 

Launch open VSCode

Search for and install the VS Code Go extension



 Open Command Palette and enter “go tools” to execute an install/update of all the necessary Golang tools



Select all the tools and install/update by clicking “OK”





1.3     Coding and Running a basic CLI app, named, “helloworld”

In VS Code, select File menu – Open Folder… and navigate to a target folder/directory for creating the new Go lang CLI app named, helloworldcliapp.



Initialize a new module for the app by opening Terminal and executing - > go mod init com.yourname/helloworld



Create/add a new source code file named, main.go, with golang code as shown below:



Have the code compiled and run by executing at the Terminal, the go command - > go run .



1.4     Import and make use of 3rd party libraries (i.e. modules/packages)

Go lang modules/packages are published and available online at https://pkg.go.dev/



 It is similar to maven central repo in Java world or npmjs.com in JavaScript world.

 

Search for a module by its name, e.g. quotes





When found, import the module using its fullname in your golang code and invoke any functions it makes available, like so:



To resolve and download/integrate the 3rd party module into the application, at the Terminal, execute the go command - 

> go mod tidy




Now, have the code compile and run once again by executing – > go run .





Some Go lang concepts: Functions -> Packages -> Modules -> Multi-modules workspace

Module: A module is a collection of Go packages stored in a file tree with a go.mod file at its root.

Package: Go programs are constructed by linking together packages. A package in turn is constructed from one or more source files that together declare constants, types, variables and functions belonging to the package and which are accessible in all files of the same package. Those elements may be exported and used in another package.

Source file: Each source file consists of a package clause defining the package to which it belongs, followed by a possibly empty set of import declarations that declare packages whose contents it wishes to use, followed by a possibly empty set of declarations of functions, types, variables, and constants.

A package clause begins each source file and defines the package to which the file belongs.


1.5    Build a binary executable

The ‘go run’ command is useful for compiling and test-running go lang program during development. Upon code completion, to create a binary executable that can be deployed and run, do the following:

 

First, introducing two additional commands for building and deploying go lang application code:

  • The go build command compiles the packages, along with their dependencies (thereby creating a binary executable), but it doesn't install (or deploy) the created binary executable to some target location.
  • The go install command compiles the packages, builds/creates binary executable and installs (i.e. deploys or copies) the artifact onto a target location, from where the program/application will execute.

On the Terminal, execute the go build command, to compile source code and dependencies and create binary executable artifact:

Ø go build


Test-run the helloworld.exe



Configure/Add a target directory where the “go install” command will install/deploy built binary executables, by running the command (In other words, set the GOBIN go environment variable):

Ø go env -w GOBIN=c:\golang\my-apps



To build and install/deploy the binary executable, run the go install command

Ø go install



// -- The End --//

Comments