Introduction to Go
Tags: go
Published: 2022-02-22 05:10:46

- Go is a systems-level programming language for large, distributed systems and highly-scalable network servers.
- Go was created at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thomson.
- Go combines the safety and performance of statically-typed compiled language (e.g. C++, C#, Java) with expressiveness and convenience of dynamically-types interpreted language (e.g. Perl, Python, JavaScript).
- Go can be used for server-side web development, network-based applications, cloud-native development.

![Robert Griesemer, Rob Pike, and Ken Thompson](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/73ium4b17ucmf98z6huk.png)

## Installation of Go on Windows

- Visit https://go.dev/and download the latest version.
- Installation is a simple wizard-based process.
- You may choose the installation folder.
- After successful installation, you can check the Go installation by opening a command prompt and type go -version

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzpsypwak09w40znf43e.png)
- The Go installer creates a GOPATH environment variable and by default set it to C:\Users\USER_NAME\go.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v7hjbrphhxzaeksgxtrx.png)

- GOPATH points to Go Workspaces. 
- Go workspace is hierarchy of folders to manage source files, compiled binaries and cached objects for faster compilation.
- There are three directories inside the workspace.
- bin: location of compiled executable programs built by Go 
- pkg: location of compiled package code
- src: location of Go source code
- For practice purpose lets create a workspace for Go applications e.g. Practice.
- To tell Go about our new workspace set GOPATH to Practice folder. Right click My Computer and select Properties. On properties screen select Advanced System Settings.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6hpvsqpfz62n0tichc5.png)

- On System properties window click Environment Variables.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzeou9eu77stdct6hfcj.png)

- In next dialog select GOPATH and click Edit button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d112qq0vr38kcncfj4g7.png)

- Replace variable value with path of your workspace (E:\Edu\Go\Practice) and click Ok. Close variables dialog and then System properties dialog. To verify that new value is assigned to GOPATH;  run the following command from Command prompt.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jc0v4j3h974itx0lel2.png)

## Installation of Visual Studio Code on Windows 

- Visual Studio Code is an editor which you can use for writing Go programs.
- To download Visual Studio Code visit the website https://code.visualstudio.com/Download
- Once the download is complete, run the exe to install Visual Studio Code.
- On the Select Addition Tasks dialog check all the available options and click next.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ew5vqs6sudp38fbz2e1n.png)

- On next dialog click install button and wait for the installation to finish.
- Launch Visual Studio Code and bring up the Extensions view by clicking on the Extensions icon in the Activity Bar or use shortcut (Ctrl+Shift+X).
- Search and install the Go extension.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ao8o1uehm903al1dnqd1.png)

- In Visual Studio Code, open Command Palette's from Help > Show All Commands or use shortcut (Ctrl+Shift+P) and update Go tools by typing Go: Install/Update tools and checking all the tools shown. Wait for the Go tools to finish updating. VS Code Go will install the tools to your GOPATH/bin by default.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/669s6alcaakogllvd6cm.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0g5xh3pyq10m04yod1e2.png)
  

## Hello World Go Program

- To open Practice folder (workspace created earlier) in Visual Studio click Open Folder from File menu and select Practice directory. 
- Right click Practice folder and create src folder. In src folder create HelloWorld directory for our hello world app.
- In HelloWorld directory create a main.go file and type a simple program to print hello world. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bn6qk5x7be36p3qxkjnx.png)

> The package keyword declares the package with the name “main”. Package is the collection of source files in the same directory that are compiled together. There are two types of Go programs; executables and libraries. An executable package requires to be created with package name main and main function declaration.

> The import statement includes a package into our program. The fmt package (shorthand for format) implements formatting for input and output. 

> All functions start with func keyword. The main function is special because it gets called when program is executed. The Println function of fmt package prints the string to the standard output (output of terminal).

- At very first line we are faced with error “go.mod file not found in current directory or any parent directory; see 'go help modules'”. 
- Go.mod file specifies a module name, dependencies, and minimal versions. The go mod init command initializes and writes a new go.mod file in the current directory and also creates pkg folder in our workspace.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6r9h74s5o3px2m79i3w.png)

- There are three ways to run Go program.
- go run command compiles the code into a binary file, places it in a temporary directory, executes this binary file, and deletes it after your program finishes.
- go build command compiles the code into a binary file and places it in the current directory without executing it.
- go install command compiles the code into a binary file, creates GOPATH /bin directory and places exe in it without executing it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fmk9960b5lvqhfjr6vhz.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75nnwwikjnk3ua02n0vm.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3xo321nsamlpu3x4m8a.png)