Part 1: Hello Gopher

Part 1: Hello Gopher

Learn about package, main, fmt.Println !

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.

  1. 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 package main is the entry point of every Go executable program.
  2. 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" imports fmt package that contains methods to interact with stdin & stdout. Package fmt implements formatted I/O with functions analogous to C's printf and scanf.
  3. func main() {...}

    • func keyword is used to declare a function in Go. As already said, every program in Go starts with main function. Function contains statements wrapped inside {}.
  4. 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") invokes Println function with "Hello Gopher" as a parameter. Println stands for print line which prints and adds a line break at the end.

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.

Did you find this article valuable?

Support Akhil by becoming a sponsor. Any amount is appreciated!