The Go Language – Writing first program
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
- 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
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
- 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")
}
- 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
- As our go program get build successfully, executed file is generated at same location as
- We are ready to execute our first Go executable as
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 🙂
2 Responses
[…] The Go Language – Writing first program […]
[…] The Go Language – Writing first program […]