What is Go or Golang?
Go is an expressive, concise, clean, and efficient programming language. Go is a fast, statically typed, compiled language but feels like a dynamically typed, interpreted language. At times it resembles C in terms of Syntax.
Installing Go
We are not going to discuss specific steps of installation as it is already mentioned on golang.org. In this series, we are going to use replit.com for writing our programs.
Write a program to print "Hello Gopher" on stdout
package main
import "fmt"
func main() {
fmt.Println("Hello Gopher")
}
//=> go run main.go
Hello Gopher
Save the above as main.go
and run go run main.go
to see the output.
Let's understand what each specific statement means.
package main
- usually, in computer science a package is a collection of shared functions/methods.
package
keyword in Go is used to name a package.main
package in Go tells the Go compiler that the package should compile as an executable program instead of a shared library.- the
main
function in the packagemain
is the entry point of every Go executable program.
import "fmt"
import
keyword with a<package name>
tells the compiler that the program is going to use an external package for using the functions defined in that package.import "fmt"
importsfmt
package that contains methods to interact withstdin
&stdout
. Packagefmt
implements formatted I/O with functions analogous to C'sprintf
andscanf
.
func main() {...}
func
keyword is used to declare a function in Go. As already said, every program in Go starts withmain
function. Function contains statements wrapped inside{}
.
fmt.Println("Hello Gopher")
- we already know that
fmt
is a package containing functions that can be used in any other package.fmt.Println("Hello Gopher")
invokesPrintln
function with"Hello Gopher"
as a parameter.Println
stands forprint line
which prints and adds a line break at the end.
- we already know that
How to run a Go program
go run main.go
compiles and runs a Go program.go build -o <output-filename>
compiles a Go program and creates an executable output file with the name specified for-o
.
That is it for this part, in the next part we will solve few simple mathematical problems in Go and get to know about DataTypes and Variables in Go.