The First Program in Golang - Golang Hello World
Overview
Golang is as efficient as C/C++/Java and its syntax as simple as Python/Perl. Hence it’s a win-win situation for both humans and processors. In this article, we will learn about the fundamentals of Golang in depth with examples.
Introduction
Golang is a statically typed compiled programming language with in-built support for garbage collection and concurrent programming. It was designed by Ken Thompson and the team and publicly released in Nov 2009.
Golang Features
- Golang programs are simple, concise, and type-safe.
- Golang is a statically typed language.
- Golang is a compiled language.
- Golang performs automatic garbage collection.
- Golang comes with a powerful library that is distributed as packages.
Features of Golang that Are Not Supported
- No classes
- No constructors
- No inheritance
- No exceptions
Compilation of Golang File
In order to run a go program, a go compiler is needed. Following are the steps in which the go program should be compiled:
- Save the program with the .go extension for instance if the file name is “hello” then save it as hello.go
- Now run the file saved that is, run the hello.go file by using the command: $ go run hello.go
- The output should be printed on the command line after running the file.
Hello Scaler Code in Golang
Write a simple hello scaler in go program.
Output:
Line by Line Description of The Code
Package name:
- Every go file must start with package_name.
- There must be a package main and func main in the program.
- If the package name is not “main” and if we try to run then we get a runtime error that says: go run: cannot run non-main package.
import “fmt”:
- Import “fmt” will import fmt package to print text to the standard output using Println()/Print()/ Printf().
- If we import “fmt” but we do not use it then we get a compile-time error that says: imported and not used: “fmt”.
main():
- Function main() is the place from where the execution of the program starts and should always reside in the main package.
- Function main() should be there in package main else will get a compile-time error saying: function main is undeclared in the main package.
Running Hello Scaler Code(Run Command in The Terminal)
Go tool:- go is a tool for managing Go source code.
The usage is: go <command> [arguments]
Commands are:
- version - prints go version.
- run - compile and run go programming.
- build - compile packages and dependencies into an executable. file
- get - download and install packages and dependencies.
- install - compile and install packages and dependencies.
Conclusion
- Golang is a statically typed compiled programming language with in-built support for garbage collection and concurrent programming.
- It was designed by Ken Thompson and the team and publicly released in Nov 2009.
- Compilation of Golang file:
- Save the program with the .go extension for instance if the file name is “test” then save it as test.go
- Now run the file saved that is, run test.go file by using the command: $ go run first.go
- The output should be printed on the command line after running the file.
- import “fmt”: Import “fmt” will import fmt package to print text to the standard output using Println()/Print()/ Printf().
- main(): Function main() is the place from where the execution of the program starts and should always reside in the main package.