How to write Hello World First Program example | Golang Tutorials-
In this blog post, we’ll go through the essentials of the Hello World example in the Go programming language.
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 a 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 the command
- go build the command
Now, go ahead, compile and run the program.
The compilation step must be done first. 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
dir
HelloWorld HelloWorld.go
This command compiles the code and generates an executable file. To run the program, We need to give an executable via the command line
/HelloWorld
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.
Golang 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](/2018/12/golang-tutorials-comments-in-go-language.html)
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, Comments begin with\\
, followed by comment text. 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 with /*
, followed by comment text in multiple lines and ends with */
/*
* 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 a 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 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 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.
Conclusion
You can write a sample hello world program in Go using the content of this article. More details about GoLang🔗 can be found here.