TheSourcePedia's Blog

Follow

TheSourcePedia's Blog

Follow
Preliminary Go Language for Beginners

Preliminary Go Language for Beginners

Introduction to Go Programming Language, Features, Installation, Setup and Basic Example.

Piyush Goyani's photo
Piyush Goyani
·Oct 19, 2022·

3 min read

Play this article

Table of contents

  • Installation
  • Demo
  • Conclusion

Go is the programming language that was Invented and backed by Google and is now used by many companies and programmers worldwide.

There aren't any use-case limitations with Go to build something as you can build anything with it, But it's mostly used for CLI Tooling, Network Services and APIs, Web applications and DevOps.

The Go Language is known for the following features:

  • Performance
  • Built-in Concurrency Support
  • Safety
  • Garbage Collection
  • Scalability
  • Portability / Cross Platform
  • Binary Generation
  • Standard Library and Package Management
  • Built-in Testing Support

The language is not intended for replacement of any other languages like C, C++, Python, Rust or any other as these languages have their own beauty and places in the market and people who adopted them for a long time.

Let's dive into installation and Basic Go Code.

Installation

Download Go

Download the Go archive from official site as per your choice. Here, I'm using Linux so the rest steps are accordingly.

Go to the downloaded directory and run this command from the terminal.

sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.19.2.linux-amd64.tar.gz

This will remove the old installation of Go and extract the downloaded version to the /usr/local directory.

Add PATH

Edit .bashrc file and sudo nano .bashrc add PATH and save. export PATH=$PATH:/usr/local/go/bin

Check the installed version of GO with the following command.

go version
// go version go1.19.2 linux/amd64

Demo

Create a demo project directory and go inside the directory.

mkdir demo && cd demo

Run the below command to enable Dependency tracking in your project.

go mod init example/demo

This will create a file named go.mod with the below content.

module example/demo

go 1.19

This file is generally used to manage internal and external dependencies, which stay within the project.

Create init.go file with your favourite text editor,

nano init.go

and paste the below code and save.

package main

import "fmt"

func main() {
    fmt.Println("First program of GoLang!")
}

Explaination

package main: It declares the main package, which may contain all sub-functionality within the same directory linked to this file.

import "fmt": Importing fmt, one of the standard library packages of Go, which contains functionality for console output and text formatting.

func main(): Defining main() function, which by default called first when program get executed, similar like main in C/C++.

Running Go Code

To run the Go program execute the below command in the terminal and see Output.

go run .
// First program of GoLang!

Conclusion

I hope you enjoyed this article. Stay tuned and subscribe for more Go Language posts and other useful tutorials.

Did you find this article valuable?

Support TheSourcePedia's Blog by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this