GO Programming
MOD 1:
- P.Priyanandini
Languages that influenced Go:
Go is a procedural programming language. It was developed in 2007 by
Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in
2009 as an open-source programming language. Programs are assembled by
using packages, for efficient management of dependencies. This language
also supports an environment adopting patterns alike to dynamic languages.
Go is a statically typed, concurrent, and garbage-collected programming
language created at Google in 2009. It is designed to be simple, efficient, and
easy to learn, making it a popular choice for building scalable network
services, web applications, and command-line tools.
Go is known for its support for concurrency, which is the ability to run
multiple tasks simultaneously. Concurrency is achieved in Go through the use
of Goroutines and Channels, which allow you to write code that can run
multiple operations at the same time. This makes Go an ideal choice for
building high-performance and scalable network services, as well as for
solving complex computational problems.
Go is influenced by C
programming in Go:
Go (also referred as Golang) is a procedural and statically-typed
programming language having syntax similar to C language. It provides a rich
standard library, garbage collection, and dynamic-typing capability.
This language also has key-value maps, length arrays features which are
advance level built-in types. Go is the most powerful, performant, and
scalable programming language for creating web applications, web APIs,
micro-services, and other distributed services. It is one of the
fastest-growing, highest-paying programming languages in America.
Evolution of Golang:
The Golang was designed by Robert Griesemer, Rob Pike, and Ken
Thompson in 2007 at Google. It was launched in November 2009 as an
open-source programming language. The main purpose of designing Golang
was to solve the problems of other languages by keeping their useful
features. In March 2012, its Version 1.0 was released. The latest version is
1.11.1 which released in 2018.
Why Golang?
The main purpose of designing Golang was to eliminate the problems of
existing languages. So we started with the problems that we are facing with
Python, Java, C/C++ languages:
● Python – “Easy to use, but slow” : Python is easy but slow as it is
an interpreted language that makes the code slow.
● Java – “Complex type system” : Java has a complex type system
due to it additional features which makes it more complex and slow.
● C/C++ – “Complex type system as well as slow compilation time”
: C/C++ was designed when the computer does not have that much
space so the developer decided to optimize the compiler by using
the minimal amount of space which results in sluggish compiler
time.
● Also, all these languages were designed when multi-threading
applications were rare, so not much effective to highly scalable,
concurrent and parallel applications.
Features of Golang:
● Strong and Statically Type language: Go is Strong as well as
Statically type language. Strong means once you created some
variable using some data type then for the whole application it will
remain type. Statically means all the variables have to define at
compile time.
● Excellent Community: Golang have an excellent community for
issue tracking and solution.
● Fast Compilations: Golang will includes only that packages which
will import directly in the code.
● Garbage collections: Automatic garbage collection of Go is very
fast.
● Built-in Concurrency: Go has the built-in feature of concurrency.
Using Go Routines and channels you can handle the concurrency
very easily and effectively.
Installation and runtime environment:
https://www.geeksforgeeks.org/how-to-install-go-on-windows/
?ref=lbp
File naming in GO:
There's a few guidelines to follow.
1. File names that begin with "." or "_" are ignored by the go tool
2. Files with the suffix _test.go are only compiled and run by the go test
tool.
3. Files with os and architecture specific suffixes automatically follow those
same constraints, e.g. name_linux.go will only build on linux,
name_amd64.go will only build on amd64. This is the same as having a
//+build amd64 line at the top of the file
Elementary datatypes:
https://www.geeksforgeeks.org/data-types-in-go/
Keywords in Go Language:
The following table contains keywords present in Go language:
Keywor Description
d
break It is used to break the execution of the loop or in simpler terms, we can
say that is used to terminate the loop.
case The case keyword is used inside the switch statements, and it is used to
match a certain condition that we passed in the switch statement.
chan The chan keyword is used to create a channel in Go. A channel is a way
with which goroutines communicate with each other.
contin The continue keyword is used to pass the control back to the starting of
ue the loop once encountered.
const The const keyword is used to create a constant in Go.
defaul The default keyword is used inside the switch statements and is used to
t denote the case when none of the above-mentioned cases in the switch
statements satisfies the given conditions.
defer The defer keyword is used when we want certain things to work after the
completion of the given function.
else The else keyword or sometimes also known as the else clause is used in
conditional branching, and mainly inside the If/Else statements in Go. It
is used when we want to run a certain code that doesn't satisfy the if
condition.
fallth The fallthrough keyword in Go is used in the switch statements. In Go,
rough the switch statements are a bit different from traditional ones. As there's
no automatic fall through to the next case, so we make use of the
fallthrough keyword to provide the same.
for The for keyword is used in looping. We can create a traditional for loop
with the help of the for a keyword, or we can also run a while looking
alike loop with the help of the for a keyword.
func The func keyword is used to create a function in Go. It is generally
followed by the name of the function we want to declare
go The go keyword is used when we want to run asynchronous code in our
program. It creates a goroutine and the code becomes asynchronous in
nature
goto The goto keyword in Go is used when we want an unconditional jump to
a labeled statement in the same function
if The if keyword is used when we want to create a conditional check.
import The import keyword is used when want to import certain third-party or
Go standard library packages into our code. The import statement is
generally followed by the name of the package that we want to make use
of in our program.
interf The interface keyword is used in Go when we want to create a named
ace collection of method signatures.
map The map keyword is used to create a map data structure in Go. A map is
a collection of key-value pairs in Go.
packag The package keyword in Go is used to declare the name of the package
e for our program. The name of the package is generally main if it is an
executable or else it is the name of the folder it is in.
range The range keyword or sometimes also known as range clause is used
mainly when we are iterating over a ceratin type in Go. It is used with the
for keyword when we want to iterate over an iterable object.
return The return keyword or sometimes also known as a return statement is
used mainly in two cases. The first case in which we want to return from
the given loop, and the case is generally when we want to return some
values when a particular loop exits or terminates.
select The select keyword is used when we want to wait on multiple Go channel
operations.
struct The struct keyword is used when we want to create a struct in Go
switch The switch keyword is used to create a switch statement in Go. A switch
statement is a conditional statement
type The type keyword is used when we want to create a new type. The
process of creating a new type is called type definition
var The var keyword is used when we want to create variables in Go.
Identifiers in GO:
Rules For Naming Identifiers
There are some rules that a programmer must follow to construct a valid
Go identifier. If the programmer does not follow these rules, then
compile-time errors will occur. These errors will interrupt the execution of
the program.
The name of an identifier can be a combination of alphabets,
underscore (_), and digits.
The first letter of a Go identifier must be an alphabet or an
underscore (_). You can use both lowercase and uppercase
alphabets.
The first letter of a Go identifier must not be a digit. The digit
from 0 to 9 can only be used after the first letter.
The name of the Go identifier is case-sensitive. It means that an
identifier must be typed with a consistent capitalization of
letters in the entire program.
The keywords (or reserved words) are not allowed to be used as
an identifier.
In Go programming, there is no rule on how many letters an
identifier can have. But it is advisable not to use more than 15
letters.
Now let's look into the table given below to understand which are the valid
identifiers and which are not.
Sample Status of Rule
Validation
hello valid The first letter must be an alphabet.
_hello valid Underscore can be used as the first letter.
Hello valid Both lowercase and uppercase alphabets
can be used.
hEllo valid Both lowercase and uppercase alphabets
can be used.
hello123 valid The First letter must be an alphabet.
Digits can be used after the first
letter.
hello_world valid The name of an identifier can be a
combination of alphabets, underscore (_),
and digits.
123hello not valid The first letter should not be a digit.
if not valid The keywords are not allowed to be used as
an identifier.
import not valid The keywords are not allowed to be used as
an identifier.
Constants in GO:
https://www.geeksforgeeks.org/constants-go-language/
Operators in GO:
https://www.geeksforgeeks.org/go-operators/
Strings in GO:
https://www.geeksforgeeks.org/strings-in-golang/
Time and date in GO:
In Go language, time packages supplies functionality for determining as well
as viewing time. The Time.Date() function in Go language is used to check
the year, month, and day in which the stated “t” presents itself. Moreover, this
function is defined under the time package. Here, you need to import the
“time” package in order to use these functions.
Syntax:
func (t Time) Date() (year int, month Month, day int)
Here, “t” is the stated time.
Return Value: It returns year, month and day of the stated “t”.
package main
import "fmt"
import "time"
// Calling main
func main() {
// Declaring t in UTC
t := time.Date(2020, 5, 6, 11, 45, 04, 0, time.UTC)
// Calling Date method
yyyy, mm, dd := t.Date()
// Prints year
fmt.Printf("The stated year is: %v\n", yyyy)
// Prints month
fmt.Printf("The stated month is: %v\n", mm)
// Prints day
fmt.Printf("The stated day is: %v\n", dd)
}
Output:
The stated year is: 2020
The stated month is: May
The stated day is: 6
Pointers in GO:
https://www.tutorialspoint.com/go/go_pointers.htm