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

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.

Defer Keyword in Golang

Defer Keyword in Golang

In Go, defer statements delay execution of a function or method or an anonymous method until the nearby functions return.

Methods in Golang

Methods in Golang

Methods in Golang are like functions but with one major difference: they have a receiver argument, which allows access to the properties of the receiver.

How many kilometers is a light year, how many earth years?

How many kilometers is a light year, how many earth years?

How many kilometers are 120 light years? How many kilometers are 40 light years? Let's find out together about a light year to find the answer!

Best time to post Instagram Reels

Best time to post Instagram Reels

The timing of your Instagram Reels can make the difference between massive engagement and dead silence. Let’s find out the best times to post on Instagram Reels.

Status about dogs, funny and humorous captions about pets

Status about dogs, funny and humorous captions about pets

There are many funny captions about dogs on the Internet. This article will gather for you the funniest, most humorous, and most interesting poems about dogs.

How to change NAT type on Windows 11/10

How to change NAT type on Windows 11/10

You will have the best gaming experience if you use Open NAT or at least Moderate NAT. That is why you need to learn how to change the NAT type.

8 Features Apple Notes Is Still Missing

8 Features Apple Notes Is Still Missing

After several updates, Apple Notes still lacks some features that could make the app even better.

4 Practical Uses of Password Managers You Didnt Know About

4 Practical Uses of Password Managers You Didnt Know About

You probably know that password managers are great for storing your online logins. But did you know they have many other great uses?

What do the heart colors on Messenger mean?

What do the heart colors on Messenger mean?

With the Messenger heart icon, you can choose different colors to use for different situations to express what you want to say to the other person.

How to Message Meta AI in WhatsApp Groups

How to Message Meta AI in WhatsApp Groups

Not only can you use Meta AI with individual WhatsApp messages, you can now send Meta AI messages in WhatsApp groups.

NVIDIA CEO: AI will replace humans in specific jobs with 1000 times higher productivity

NVIDIA CEO: AI will replace humans in specific jobs with 1000 times higher productivity

The concept of artificial intelligence - AI is becoming more and more popular, which means that this superior technology is gradually being effectively deployed in many fields as well as in all aspects of modern life.

AI Robots Can Be Jailbroken, and the Consequences Will Be Dire

AI Robots Can Be Jailbroken, and the Consequences Will Be Dire

Researchers at Penn Engineering (USA) are said to have discovered previously unidentified security vulnerabilities in a number of AI-controlled robotic platforms.

Difference between WAN port and LAN port

Difference between WAN port and LAN port

If you look at your wireless router, you may see the abbreviations LAN and WAN, usually located next to some ports on the device. LAN stands for Local Area Network and WAN stands for Wide Area Network. So what is the difference between these two types of ports? Let's find out with Quantrimang through the following article!

Why should you stop using your phone to edit photos?

Why should you stop using your phone to edit photos?

Smartphones are great for editing photos, but you should consider using a computer instead. There's a reason the pros do things differently.

How to Share Large Files Right in Your Browser Using FilePizza

How to Share Large Files Right in Your Browser Using FilePizza

Thanks to FilePizza, you can finally share large files without the need for a middleman.

The 20 longest non-stop flights in the world by distance

The 20 longest non-stop flights in the world by distance

The 20 longest flights in the world by distance (all longer than 12,000km and with flight times ranging from 15 hours 5 minutes to 18 hours 50 minutes).

Watch Ironman race at speed in Dubai

Watch Ironman race at speed in Dubai

The world's first Jet Suit Race was held in Dubai by Gravity Industries, a pioneer in jet suits, in partnership with the Dubai Sports Council.