0% found this document useful (0 votes)
221 views7 pages

Introduction To Golang

The document introduces the Go programming language. It was created at Google by Robert Griesemer, Rob Pike, and Ken Thompson to address limitations in existing languages like C++, Java, and Python. Go aims to be simple, fast, support parallel computing with multithreading in mind, have automatic memory management, and fast compile times. Popular projects built with Go include Docker, Kubernetes, Terraform, and more. The document then provides examples of basic Go programs like Hello World, prime number checking, array printing, linear search, and using structures.

Uploaded by

mamta yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
221 views7 pages

Introduction To Golang

The document introduces the Go programming language. It was created at Google by Robert Griesemer, Rob Pike, and Ken Thompson to address limitations in existing languages like C++, Java, and Python. Go aims to be simple, fast, support parallel computing with multithreading in mind, have automatic memory management, and fast compile times. Popular projects built with Go include Docker, Kubernetes, Terraform, and more. The document then provides examples of basic Go programs like Hello World, prime number checking, array printing, linear search, and using structures.

Uploaded by

mamta yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Introduction to Golang

- Berin Aniesh.

Go is an open source, strong and statically typed general purpose


programming language, developed at Google by Robert Griesemer, Rob Pike,
and Ken Thompson.

In this article, we will learn why golang exists and also learn the basic
syntax of the Go programming language.

Why create a new programming language when dozens exist already

When golang was created, the most popular languages at Google were
C/C++, Java and Python and each language had its own problems.

● C/C++ is difficult to program in, has long compile times, has a complex
type system, and is not modern because it has to maintain compatibility
with old code bases.
● Java again has a complex type system, uses large amounts of memory
and is slow.
● Python is an interpreted language, which is obviously too slow for any
performance intensive tasks.

Also, most of the languages available at the time of creation of Go were


not built keeping multithreading in mind. But Google’s use case was mostly
multithreaded.

What does Go offer

● Simple by Design
● Speed - Go is a compiled language which implies it is fast
● Parallel Computing - Go is built with multithreading in mind
● Garbage collected - No need to worry about memory management
● Extremely fast compile times
● Strong community
● Large number of libraries available for many use cases.
● Compiles to a single standalone binary - no dependency hell or external
libraries needed.

Popular projects using Go

● Docker
● Kubernetes
● Terraform
● SoundCloud.com
● Uber
● Hugo

An extensive list can be found here:


https://github.com/golang/go/wiki/GoUsers

Since Go is a general purpose programming language, it’s use cases


can be very versatile. It is particularly suited for programming networked
servers in backend development. Go is one of the fastest growing languages
and so learning Go can open a lot of opportunities for anyone learning it.
Alright, Enough Talk, Let’s write our first Go program!

Head on to https://play.golang.org to write your first go program without


installing anything on your PC.

Program 1: Hello world in go

First of all, every go program is structured into packages and so every


Go file should be a part of a package. Package main is a special package as it
serves as the entry point to any go application.

The second statement imports the fmt library which is needed to print
text to the console.

Inside the main function, we print the text “Hello world”.


Program 2: Printing all prime numbers between two numbers

To understand the logic of the program, we request you to watch our


java videos. Here, we will be focusing on syntax mainly.

A few things to keep in mind are

● In golang, we cannot declare a variable and not use it or import a library


and not use it, which means if we import a library or declare a variable,
you should use it.
● a := val means we are initializing the variable a and assigning it the
value of val.
● math.Sqrt takes input of a floating point number and so our integer is
converted into a floating point before passing it to math.Sqrt.
● The variable type declaration is done after the name of the variable.
● Code blocks are placed inside curly braces, which controls the flow of
the program.
Program 3: Display the given array as a bar chart in the console

Solving the below DSA question in Go

https://www.pepcoding.com/resources/online-java-foundation/function-and-
arrays/bar-chart-official/ojquestion

Few things to notice in this program

● len(arr) gives the length of the array arr.


● The else statement should be in the same line as the closing bracket of
the if statement.
● Also take a look at the syntax of the initialization and declaration of the
array. Here, arr is the name of the array, int is the data type.
Program 4: Linear Search in Go

The above program implements a simple linear search in Go.

The interesting point to notice here is line number four. This is the same
as the “for each” loop in most other languages but the index is not given a
variable name but an underscore because Go doesn’t allow unused variables.
If we were to use a variable name in place of the underscore, we have no use
for that variable and Go doesn’t allow us to compile the program.

Also note that the function linear_search has a return type of boolean,
which is specified at the end of the function declaration.
Program 5: Structures in go

In this program, we will create a structure in Go to store the data of a


car and we will print it to the console.

Note that there are no object constructors or method overloading. They are
left out on purpose in Go to maintain simplicity.

Fun fact:

Golang uses a teal gopher as its mascot.

The code for the above programs can be found in the following github gist.

https://gist.github.com/berinaniesh/79dfb1378aa18c2e4264bd97fa256895

You might also like