The Go Language – Writing first program

First Go program - executed successfully
First Go program - executed successfully

Hi All,

Greetings for the day!!!

As in this week we started article series on Go language,we will move ahead. In our previous first article – The Go Language – Introduction Part 1 we started discussion about Go language. Which is introductory part.

In this article we go for practical aspect, will set up environment and will do our first program

Take away from this article

  • Setting up environment for Go programming / Installing Go
  • How to write first program in Go language
  • Using VSCode for Go programs

Setting up environment for Go programming

  • Install the latest version of Go
  • We could download latest version of Go from – https://go.dev/doc/install
  • Here we will install on our Windows 10 box
  • Once downloaded Go for Windows – “go1.18.1.windows-amd64.msi”
  • Execute the “go1.18.1.windows-amd64.msi” file as

Starting with Go - Installing Go on Windows 10 box
fig : Starting with Go – Installing Go on Windows 10 box

Starting with Go - Installing Go on Windows 10 box
fig : Starting with Go – Installing Go on Windows 10 box

Starting with Go - Installing Go on Windows 10 box
fig : Starting with Go – Installing Go on Windows 10 box

Starting with Go - Installing Go on Windows 10 box
fig : Starting with Go – Installing Go on Windows 10 box

Starting with Go - Installing Go on Windows 10 box
fig : Starting with Go – Installing Go on Windows 10 box

Starting with Go - Go installation completed on Windows 10 box
fig : Starting with Go – Go installation completed on Windows 10 box

  • Once Go installation completed successfully, we could verify by using Command Prompt
  • Open the command prompt and verify the command – go version as

go version

Starting with Go - Go installation completed successfully on Windows 10 box
fig : Starting with Go – Go installation completed successfully on Windows 10 box

Writing our first Go program

  • We could use any text editor for writing Go program, there are few popular options – VSCode, GoLand (this is paid), Vim etc.
  • I’ll prefer to write in VSCode, my favorite
  • In VSCode we will create a file – “godemo_1_helloknowledgejunction.go”
  • We have Go language extension in VSCode as

Writing first Go program - Installing VSCode extension
fig : Writing first Go program – Installing VSCode extension

Writing first Go program - Installing VSCode extension
fig : Writing first Go program – Installing VSCode extension
  • As we install VSCode Go extension successfully, we are ready to write our first simple program as

package main

import "fmt"

func main() {
	fmt.Println("LIFE IS BEAUTIFUL")
}

Starting with Go - Our first Go program
fig : Starting with Go – Our first Go program

  • We will walkthrough the program in below steps
  • We will first build our program using – go build command as in above fig
  • SYNTAX : go build <go file>

go build .\godemo_1_hellloknowledgejunction.go

Starting with Go - Our first Go program build successfully
fig : Starting with Go – Our first Go program build successfully
  • As our go program get build successfully, executed file is generated at same location as

fig : Starting with Go – Go program build successfully. Executable file generated

  • We are ready to execute our first Go executable as

First Go program  - executed successfully
fig : First Go program – executed successfully

Detailed walk through the our first program

  • Package main
    • Every Go program must start with package declaration
    • Package main is executable program
    • Package main tells the compiler that package must compile in executable
  • func main() – this is were program execution starts
  • On line 3 we are importing package – “fmt” , this package contain functions for formatting text, printing output to console.
  • fmt” package is installed when we install “Go
  • Println ()”
    • Println() function is from “fmt” package
    • This is output function
    • This function prints one more values separated by spaces

Thanks for reading!!! Feel free to discuss in case any issue / suggestions / thoughts !!!

HAVE A GREAT TIME AHEAD !!! LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

1 Response

  1. May 11, 2022

    […] The Go Language – Writing first program […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: