Table of contents
Why Go🤔Why not PHP, or Python, or Node.js, or whatever!
- Compiles to a single binary file
- No runtimes to worry about
- Statically typed, so no surprises at run time
- Concurrency
- Cross-platform
- Excellent package management & testing built-in
So if you are new to programming, you might be wondering what these things are! Not to worry about it, We are going from the very basics🤩
About Go:
Also known as Golang, Go is a programming language designed by Robert Griesemer, Rob Pike, and Ken Thompson. It is an open-source programming language that makes it easy to build simple, reliable, and efficient software solutions. Go is a statically typed and compiled programming language. Statically typed means that variable types are explicitly declared and thus are determined at compile time. Whereas, by compiled language, we mean a language that translates source code to machine code before execution.
Many big-tech companies use Go👇
Installing Golang
To download and install Golang, you can check Click here to install
If you are a mac, Linux, or Windows user you can download and install according to yourself
Read the instructions and go ahead with the flow related to the installation (Like as we're doing from our childhood😁)
Now, once you see that the installation has been completed, head over to the terminal.
To verify if Go has been installed successfully or not, type go version
.
If not installed some error will occur.
Your First Hello World Program🧑💻
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
Explanation:
Line 1:
- Every Go program must start with package main (a package is a way to group functions, and it's made up of all the files in the same directory)
- Syntax of declaring a package:-
write the package keyword and then the name of the package.
Line 2: import "fmt"
- import means we are including code from other packages to use in our program.
- fmt package is a short form of format, and it implements formatting for input and output.
So, import "fmt" basically means that we are importing the fmt package in our code because we need the functionalities provided by this package.
Note :- package name during the import statement is surrounded by double quotes.
Line 3: func main(){ }
- This is the main function of our main executable program includes.
- It does not take in any arguments or parameters.
- This function executes by default when you run code in the file.
Line 4: fmt.Println("Hello World")
- Here we are using "fmt" package.
.
means infmt
package there are various functions. We are usingPrintln
which means print the line.- Next one is "Hello World", This is going to print. (Anything in double quotes is a string)
Congratulations 🥳 You wrote your 1st program in GO.
Let's look into more data types and syntax of Go.
Variables & Functions <>
package main
import (
"fmt"
)
func main() {
// Declaring variable
var WhatToSay string = "Goodbye"
//printing the String
fmt.Println(WhatToSay)
//Declaring integer
var i int = 10
//printing integer
fmt.Println("Number is set to =" , i)
//calling a function:
WhatWasSaid , otherthing := something()
fmt.Println(WhatWasSaid , otherthing)
}
func something() (string , string){
return "Omkar", "2ndString"
}
Pointers
- a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory
package main
import "log"
func main() {
var myString string = "Omkar"
log.Println("myString is set to", myString)
// Passing the reference to 'myString' variable, to access its value
ChangeUsingPointer(&myString)
log.Println("After change,myString ia set to", myString)
}
func ChangeUsingPointer(str *string){ //str is a pointer variable of type string
log.Println("str is set to", str)
newValue := "Kulkarni"
*str = newValue
}
Types & Structs
- A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct
package main
import (
"fmt"
"log"
"time"
)
type User struct {
FirstName string
LastName string
Age int
PhoneNo string
Birthdate time.Time
}
type Marks struct{
English string
DBMS string
CN int
AI int
}
func main() {
User:= User {
FirstName: "Omkar",
LastName: "Kulkarni",
Age: 20,
PhoneNo: "112-121-212",
}
log.Println(User.FirstName, User.LastName)
log.Println(User.Age)
log.Println(User.Birthdate)
fmt.Println(User.PhoneNo)
Marks:=Marks{
English: "100",
CN: 90,
DBMS: "91",
AI: 99,
}
log.Println(Marks.English, Marks.CN, Marks.DBMS, Marks.AI)
}
Maps
- A map is an unordered collection of key-value pairs. Also known as an associative array, a hash table, or a dictionary, maps are used to look up a value by its associated key. Here's an example of a map in Go:
package main
import "log"
func main() {
// syntax to create and initialise the map
// just creating a map
Mymap := make(map[string]string)
Mymap["first"] = "Omkar"
Mymap["second"] = "POP"
log.Println(Mymap["first"])
log.Println(Mymap["second"])
// Declaring and initialising at the same time
myMap2 := map[string]string{
"third" : "john",
"fourth" : "Fido",
}
log.Println(myMap2["third"])
log.Println(myMap2["fourth"])
myMap3 := map[string]int{
"Age" : 20,
"Date" : 30,
}
myMap3["Age"] = 21
log.Println(myMap3["Age"])
}
Slices
- A slice is a segment of an array. Like arrays slices are indexable and have a length. Unlike arrays this length is allowed to change. Here's an example of a slice:
package main
import (
"log"
"sort"
)
type User struct {
FirstName string
LastName string
}
func main() {
//1.
myMap := make(map[string]User)
first := User{
FirstName: "Omkar",
LastName: "Kulkarni",
}
myMap["first"] = first
log.Println(myMap["first"].FirstName)
log.Println(myMap["second"].LastName)
// 2nd method:
myMap2 := map[string]User{
"first": {
FirstName: "Omkar",
LastName: "Kulkarni",
},
"second": {
FirstName: "John",
LastName: "caliber",
},
}
log.Println(myMap2["second"].FirstName)
log.Println(myMap2["second"].LastName)
//Slices
var names[] string
names = append(names, "Omkar", "John", "chad")
log.Println(names)
var nums= []int{5,7,3,1,2}
log.Println(nums)
sort.Ints(nums)
log.Println(nums)
nums2:= []int{1,2,3,4,5,6,7,8,9,10}
log.Println(nums2[5:9])
}
Decision Structures
if-else
package main
import "log"
func main() {
//if-else statement
isTrue := true
if isTrue {
log.Println("isTrue is set to" , isTrue)
} else {
log.Println("isTrue is set to" , isTrue)
}
}
switch statement
package main
import "log"
func main() {
// switch statements:
myAge := 20
switch myAge{
case 18:
log.Println("You cannot vote, Age:=", myAge)
case 19:
log.Println("You cannot vote, Age:=", myAge)
case 20:
log.Println("You can vote, Age:=", myAge)
default:
log.Println("Invalid input!")
}
}
Advantages of Golang
❤️Thank you very much for reading ❤️
If you find the blog useful don't forget to like, share and comment.
If I miss out on anything feel free to comment
Feel free to comment your views in the comment section.
Connect with me on Twitter
Happy Learning 😊!
I hope you enjoyed part 1 of Golang. Stay tuned for the next part of the blog.