Golang Tutorials-Hello World First Program example

In this blog post, we’ll go through the essentials of the Hello World example in the Go programming language.

Golang hello world first program example

Go Language First Program code

Google released Go, a popular Opensource programming language. You will write a sample Hello world program example while learning any language.

To write and run the Golang program, you’ll need the following things.

  • Go Compiler installation
  • Use any code editor, such as Sublime Text or Atom, or Visual Studio Code, to manually write the code shown below.

Golang Hello World First Sample Program Example

To run the code, you must do the following things, just as in the java programming language. You must first compile the code to generate machine-readable code that can be executed.

// This is sample Hello world programming
package main
import "fmt"
func main() {
    fmt.Println("Hello World Sample First  Program")

In this language, the above two tasks can be accomplished in a variety of ways.

  • go run command
  • go build command

Now go ahead and compile and run the program. The compilation step must be done first. The compilation is the method of translating high-level machine-readable code, such as assembly code, into low-level machine-readable code. We can use the go run or go create commands to compile the program. The go run command compiles and runs the code.

go run HelloWorld.go

**Hello World Sample First  Program**

Other ways are

go build HelloWorld.go
HelloWorld HelloWorld.go

This command compiles the code and generates an executable file. To run the program, We need to give executable via command line

Hello World Sample First  Program

We can use it for compiling and running the code using either go run or go build commands This program just prints the string to the console.

Sample program Code explanation

Here are the main components of the first program

  • Comments Declaration
  • package keyword
  • import package
  • Main Function Declaration
  • println function calling

Go code can be always read from Top to Bottom and Left to bottom. A step-by-step walkthrough of the code is given below, along with explanations. Comments Declaration Comments are used to explain a line of code or a file to the developer. The Go Compiler Execution ignores these. Comments, like the Java programming language, begin with /. The first line of the program is declared with a note. For code in Go, there are two ways to announce comments. As shown below, single-line comments may be declared.

// This is a sample Hello world program

Multiple line comments can be declared like /* comment */

* Line 1 Comment
* Line 2 Comment

package keyword with the main The following line is a package declaration, which is a package keyword. In a Go program, package declaration is always declared first. Packages are useful for grouping code under a common name that can be reused. The main package is necessary for the execution of standard libraries.

In Go Language, the package declaration is often the first line of code to be executed.

if the package is not declared in code, It gives compilation error like can’t load package: a package main: expected ‘package’, found ‘import’

can't load package: package main:
prog.go:2:1: expected 'package', found 'import'
prog.go:3:2: expected ';', found "fmt"

import package The import keyword uses to get in additional library packages into the current golang program. The standard package fmt is imported, here so that all of the classes and utilities in the current code can be used. The fmt package includes input and output stream utilities.

Function Declaration

In any programming language, functions are a set of statements that are executed in a specific order and can be grouped under a single name.

Functions take data, process it, and return the result.

None or any values uses as input, and nothing or any values uses as output.

In Golang, the keyword func uses to declare a function.

main is the name of the function which is always a starting point execution of go program code.

functions are enclosed with braces{}.

When you run the code, the main() function is always called first.

fmt.println() statement The function of the fmt package contains println() function that prints a line to the console. This function accepts a string as input and prints it to the standard console.


You can write a sample hello world program in Go using the content of this article. More details about GoLang can be found here.

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.