Go — 1

Himashi Karunathilake
3 min readJul 14, 2023

Introduction

Golang, simply known as “Go” is a compiled programming language that was designed by Google. Go is known for being a language that is simple, reliable, and efficient. It is gaining popularity among developers for its fast performance, concurrency support, and concise context.

The official website for Go provides a playground for anyone that wants to get to know it better: Go Playground — The Go Programming Language.

However, if you want to install Go in your own PC, you can download it from its website at: All releases — The Go Programming Language and follow the installation instructions at Download and install — The Go Programming Language.

To code in Go, you can use any supported IDE (Visual Studio Code in this case with the Go extension installed: Visual Studio Code — Code Editing. Redefined) and save the files with the extension “.go” (e.g., main.go).

The First Program in Go

As always, let’s try printing the “Hello World!” statement for Go as the first step.

Comments in Go

There are two types of comments in Go:

  1. Single Line Comments (//)
  2. Multi Line Comments (/* */)
// This is singe line comment.

/*
This
is
a
multi line
comment */

Declare A Package

A package can be used to group functions and / or group all the files in the same directory.

// The first program in Go.

package main

Import Library Packages

To use functions for formatting text, a standard library package of the name “fmt” will have to be imported to the file.

// The first program in Go.

package main

import "fmt"

Implement the Main Function

Implement a function that will contain the output statement. A “main” function executes by default when you run the main package.

// The first program in Go.

package main

import "fmt"

func main() {
}

Add the Output Statement

To print a statement, include the output statement within the main function.

// The first program in Go.

package main

import "fmt"

func main() {
fmt.Println("Hello World!")
}
Given below are the output statements that you can use:

fmt.Print Print the output to the console without a newline ("\n")
character appended at the end
fmt.Printf Used for formatted printing
fmt.Println Print the output to the console with a newline ("\n")
character appended at the end

Initialize the Modules

Before running the file, initialize the modules for easier management of dependencies. This could be done by typing the following command in a command prompt:

go mod init github.com/your_username/your_file_name

Run the File

  1. To run the file, open a command prompt and navigate to the file location.
  2. Type either of the following into the command prompt:
go run main.go (go run file_name)
go run . (run the first Go file that it finds)
Output of the print statement

Declaring Variables

A variable can be used to store a value. In Go, variables can be declared as follows:

// The first program in Go.

package main

import "fmt"

func main() {
// Print a single line
fmt.Println("Hello World!")

// Print an empty line
fmt.Println()

// Declare variables to store a string and a number
name := "Rachael"
age := 22
}

Printing Variables

Variables in Go can be printed as follows once declared:

// The first program in Go.

package main

import "fmt"

func main() {
// Print a single line
fmt.Println("Hello World!")

// Print an empty line
fmt.Println()

// Declare variables to store a string and a number
name := "Rachael"
age := 22

// Print the variables
fmt.Println("Name of Programmer: ", name)
fmt.Println("Age of Programmer: ", age)
}
Output for printing variables

Sign up to discover human stories that deepen your understanding of the world.

Himashi Karunathilake
Himashi Karunathilake

Written by Himashi Karunathilake

I am a cybersecurity enthusiast and writer with a passion for demystifying complex topics. Join me as I explore the ever-evolving world of cybersecurity!

No responses yet

Write a response