How to create basic program in Golang

Hello, World! is the first basic program in any programming language. You can write this first program in Golang  by following the steps below.

First, open the Go compiler. In the Go language, programs are saved with the .go extension and are UTF-8 text files.
Now, first add the main package to your program:

package main

Every program must start with a package declaration . In Go, packages are used to organize and reuse code. In Go , there are two types of programs available, executables and libraries. Executables are programs that can be run directly from the terminal. Libraries are program packages that we can reuse in our programs. Here, the main package tells the compiler that the package should be compiled into an executable program, not a shared library. This is the starting point of the program and also contains the main function .

After adding main package import “fmt” package to your program:

import(
"fmt"
)

Here, import keyword is used to import packages into the program and fmt package is used to implement formatted Input/Output using functions.

Now write code in main function to print hello world in Go language:

func main() {
    fmt.Println("!... Hello World ...!")
}

In the above lines of code, we have:

  • func : Used to create functions in Go language.
  • main : Is the main function in Go language, contains no parameters, does not return any values ​​and is called when you execute the program.
  • Println() : This method is available in fmt package and is used to display the string “ !… Hello World …! ”. Here, Println means Print line.

For example:

// Chương trình Go đầu tiên
package main

import "fmt";

// Hàm chính
func main() {

    fmt.Println("!... Hello World ...!")
}

Result:

!... Hello World ...!

How to run Golang program

To run a Go program, you need a Go compiler. Once you have a Go compiler, first create a program and save your program with the .go extension , for example, first.go . Now we run this first.go file in the go compiler using the following command, i.e.:

$ go run first.go

How to create basic program in Golang

Sign up and earn $1000 a day ⋙

Leave a Comment

Function arguments in Golang

Function arguments in Golang

Go supports two main ways to pass arguments: Pass by Value and Pass by Reference. Go uses pass by value by default.

Function returning multiple values ​​in Golang

Function returning multiple values ​​in Golang

In Go language, you are allowed to return multiple values ​​from a function, using the return statement. In other words, in a function, a return statement can return multiple values.

Switch Statement in Go

Switch Statement in Go

Golang like most other programming languages ​​has switch statement. Here is how to use switch statement in Golang.

Cases to know when using selection statements in Golang

Cases to know when using selection statements in Golang

In this article, we will learn how to use default case to avoid deadlock. But first, we will learn what is deadlock case when using select command in Golang?

Anonymous structures and fields in Golang

Anonymous structures and fields in Golang

Anonymous structs in Golang are temporary structures with no names used for one-time purposes, while anonymous fields allow embedding of unnamed fields.

How to use Rune in Golang

How to use Rune in Golang

What is Rune in Golang? How to use Rune in Golang? This article will give you the answer.

How to use operators in Golang

How to use operators in Golang

Operators allow us to perform different types of operations on operands. In Go language, operators can be classified based on their different functions.

Void identifier in Golang

Void identifier in Golang

_(underscore) in Golang is called Blank Identifier. Identifier is a user-defined name of program elements used for identification purposes.

How to Install Go on Windows

How to Install Go on Windows

Golang can be easily installed on Windows. Here is a step-by-step guide to install Golang on Windows.

Methods with the same name in Golang

Methods with the same name in Golang

Golang allows creating two or more methods with the same name in the same package, but the receivers of these methods must be of different types.

Functions in Go

Functions in Go

In Go, functions are blocks of code that perform specific tasks, which can be reused throughout the program to save memory, improve readability, and save time.

Variable Scope in Go

Variable Scope in Go

The scope of a variable in Golang determines which part of the program the variable is accessible to. In Go, all identifiers have a lexical scope that is determined at compile time.

Control Statements in Golang (if, if-else, Nested-if)

Control Statements in Golang (if, if-else, Nested-if)

What conditional statements does Golang have? How to use conditional statements in Go programming? Let's find out together!

Short variable declaration operator in Go

Short variable declaration operator in Go

The short variable declaration operator (:=) in Golang is used to create variables with a proper name and initial value. Here is what you need to know about the short variable declaration operator in Golang.

Google confirms issue with second-generation Chromecast and Chromecast Audio

Google confirms issue with second-generation Chromecast and Chromecast Audio

After a few days of confusion, Google has officially confirmed the issue with both the second-generation Chromecast and Chromecast Audio.

Update Firefox now before it becomes a security risk!

Update Firefox now before it becomes a security risk!

Unfortunately, it seems that bad guys have found a vulnerability in Firefox and are actively using it right now.

How to change design colors on Canva is very simple

How to change design colors on Canva is very simple

Canva supports you with many tools to change your design very professionally. For example, you can change the design color on Cava with just a simple click.

How to insert citations in Google Docs

How to insert citations in Google Docs

Reference citations in Google Docs make it easier for readers to refer to the content in the document. And in the process of adding citations, users can change them to suit the document.

How to Recover Locked WhatsApp Account

How to Recover Locked WhatsApp Account

WhatsApp accounts are locked for many reasons, it could be because you did something that caused Meta to lock your WhatsApp account or because of Meta.

How to Erase Any Detail in an Image with GIMP

How to Erase Any Detail in an Image with GIMP

With GIMP, often jokingly referred to as the free version of Photoshop, users can easily remove objects in the background of images with just a few tools. This technique requires a little patience to get the best results.

Latest FC Mobile Code 04/25/2025

Latest FC Mobile Code 04/25/2025

FC Mobile Code contains many rewards such as FC Point, player package, diamond package... Below we have compiled the latest FC Mobile giftcodes along with how to redeem codes to receive rewards.

Full list of latest AFK Journey codes and how to redeem codes

Full list of latest AFK Journey codes and how to redeem codes

AFK Journey Code will help players have a smoother and easier start in their journey.

Latest XSamkok Super Energy Three Kingdoms Code and how to enter code

Latest XSamkok Super Energy Three Kingdoms Code and how to enter code

Code Xsamkok Super Energy Three Kingdoms gives players a series of rewards when exchanging codes.

How to convert money amounts to words in Excel, no add-in needed, supports both 32-bit and 64-bit Excel

How to convert money amounts to words in Excel, no add-in needed, supports both 32-bit and 64-bit Excel

Convert numbers to words in Excel, convert amounts to words with Excel versions from 2003 to 2016, Excel is free like WPS Office, no need to install add-ins, supports both 32-bit and 64-bit versions.

How to calculate percentage (%) in Google Sheets

How to calculate percentage (%) in Google Sheets

What is the percentage function in Google Sheets? How to calculate percentage in Google Sheets? Let's find out with Quantrimang.com!

Download AutoCAD 2020 free license (AutoCAD Student)

Download AutoCAD 2020 free license (AutoCAD Student)

Autodesk AutoCAD 2020 has a free download program for those who are students, students or teachers with copyrighted software. Users only need to use the confirmation document and that's it.

How to perform a search on a Samsung Galaxy phone

How to perform a search on a Samsung Galaxy phone

The idea behind Samsung's system-wide internal search feature is to help users find everything they need on their device from a single area, with simple, quick operations.

How to disable guest use on Chromebook

How to disable guest use on Chromebook

When you turn this feature off, only registered accounts can use your Chromebook. It's super simple to do.

Mufasa: Is The Lion King worth watching?

Mufasa: Is The Lion King worth watching?

Mufasa: The Lion King has officially premiered worldwide. Is Mufasa: The Lion King worth your money to see in theaters?