Tuesday, April 23, 2024

Mastering Go: Part 1 - Getting Familiar

Background

This blog series is for software engineers and professionals who want to learn the GO programming language. It's also great for beginners in programming. It's written mainly for people who already know some programming terms. Unlike other tutorials, we focus on practical examples and problem-solving instead of long theories.

Prerequisite

To get the most out of this series, you should have:

  • A basic understanding of programming concepts and the ability to read simple code.
  • Some knowledge of databases.
  • Familiarity with the technical terms used in software engineering.
  • Experience using a code editor, preferably Visual Studio Code.


Environment Setup

Install VS Code

You are free to use the IDE of your choice, but throughout this series and its exercises, we will be using Visual Studio Code (VSCode). VS Code is an open-source code editor that you can freely download and use. The editor supports various programming languages, including Go.


Step 1: Download VS Code from https://code.visualstudio.com/download. Choose the download that matches your machine's operating system. For this series, I'm using MacOS. Download and save the application file (Visual Studio Code.App) for MacOS. 





Step 2.  Double-click on the .App file to launch Visual Studio Code. If you see an alert like the one below, click "Open."

 



Install Go

Step 1: Go to the website https://go.dev/doc/install and download the latest version of Go




Step 2: Double-click on the .pkg file and follow the on-screen instructions.



 



Step 3: Verify the installed version of Go using the following command in Terminal.

Command: go version 




Step 4: Install the Go language extension in VS Code.

Click on Extensions-> Type "Go"-> Select the Go Extension-> Click on “Install" and re-start the VS Code.

 




Now, you are all set with the development Environment setup for Go.


Go programming style!

Important things to remember before deep dive into go programming.

Case-sensitive language- Go is a case-sensitive language. Function, variable names starting with a Uppercase letter are considered public, and started with lower case are considered private.

Naming Conventions: Use mixed cap naming convention and avoid using underscore wherever possible. 

  • The package name will be lowercase and a single word. 
  • Source code files are single-word lowercase letters or multi-word lowercase separated by underscore.
  • Constant will be in all caps with an underscore for multi-word
  • Project folder names will be all in lowercase with an underscore for compound word

Semicolons: Go compiler (go lexer) adds semicolons to terminate statements when it finds a newline identifier. We don’t need to add a semicolon to terminate the statement.

Functions:  Go functions can return multiple values like the C# tuple functionalities.

Comments: like C#, single line comments start with double backslash “//” and multiple line comments enclosed with /* ..*/

Interfaces:  it’s a type with a set of method signatures. Go doesn’t support class-based inheritance so, an interface is used to achieve similar concepts. 


Write your first program with GO

Step 1Open the VS Code editor and create a new folder for your code/project.


Step 2. If the alert below pops up, click on “OK”



 


Step 3. To rename the untitled workspace, go to File -> Save Workspace As -> Provide a name and click Save.

 











Step 4.  To create a Go module, run the following command:


GO mod init [module name]


Go uses a go.mod file to manage dependencies and imported packages. This file must be within your project's code files to track the modules and packages being used or imported into the project.

 

 



Step 5. Add a new file “MyFirstGo.go” and write the below code 


package main


import "fmt"


func main() {

    fmt.Println("Learn programming with Sharad!")

}



Step 6. To run your first-go program, run the command in the VS code terminal


cmd: Go run . or Go run MyFirstGo.go

 


Congratulations! You've written your first Go programming language code and successfully executed it. Follow this blog for more exciting and industry-standard Go programming concepts.




Reference

1. https://go.dev/doc/tutorial/getting-started

1 comment:

  1. Clear walkthrough for getting started with Go installation.

    ReplyDelete

Mastering Go: Part 14 - Messaging with Apache Kafka(Go Implementation)

In this post, we will explore how to implement Apache Kafka messaging in Golang. Several packages are available, and the best choice depends...