Go programming language from scratch. (Module 1) - The Basics.
Today we would be learning about a new programming language called as "Go programming language" AKA "Go lang", Well, I recommend you that it's not mandatory to learn this language, unless if you are a google lover like me, then its a must.
Here is what you need to know, I'll be starting right from scratch from printing "Hello World," so you guys don't have to worry about having prior knowledge in this language, and I care a damm about it. You don't have to worry about installing the IDE or anything like that, don't worry its Google, everything is ready at your fingertips. Google has an IDE in the cloud called as the "The Go Playground" which you can find it here Go Playground, Well this is like a compiler or whatever, let's not bug our heads into it.
Less talk and more code.
Program 1: Write a program in Go lang to print "Hello World".
package main
import "fmt"
func main() {
fmt.Println("Hello, World")
}
You guys can go and type or paste this piece of code in Go playground. Don't play with the braces, you have to maintain the standard in using it in the same way because that's what Google does and that's what I do.
Explanation about the above code: Here its all about packages, each time you have to tell the package name when you want to do something new. So first you have to include the main package and then you have to import "fmt" from the main package. And every time you declare a function name you have to append it to a keyword called "func". So don't worry its a bit similar to C because Ken Thompsom is also one of the peer developers of GO. As you go by, you will be familiar with this language.
Program 2: Write a program in Go lang to print the current time.
package main
import (
"fmt"
"time"
)
func main(){
fmt.Println("Hello World")
// import time
fmt.Println("The Current time is:" time.Now())
}
Just copy this code and paste it into the Go playground and see how it works. What this actually does is that it just displays the current time. You cannot just use time, it has to be imported from a package called main. And as normal Now() is a method in order to access it you need to use time.Now(), and there you go, Give it a try !!!.
Program 3: Write a program in Go lang to print random numbers.
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("My favorite number is", rand.Intn(10))
}
Program 2: Write a program in Go lang to print the current time.
package main
import (
"fmt"
"time"
)
func main(){
fmt.Println("Hello World")
// import time
fmt.Println("The Current time is:" time.Now())
}
Just copy this code and paste it into the Go playground and see how it works. What this actually does is that it just displays the current time. You cannot just use time, it has to be imported from a package called main. And as normal Now() is a method in order to access it you need to use time.Now(), and there you go, Give it a try !!!.
Program 3: Write a program in Go lang to print random numbers.
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("My favorite number is", rand.Intn(10))
}
But there is one problem here, each time you run this program you get the same result. So here is a trick but it doesn't work, there is a built-in function called Seed which takes the parameter as time.Now(), but unfortunately this is not working, hope it would be fixed. In the above program, all we do is import the math/rand (Don't try to import only math or rand, you will end up in getting an error). In the main function, we use something as rand.Intn(10), this is nothing but rand.Intn() is a function which returns the random Integer number between the range provided as a parameter.
Program 4: Write a program in Go lang to find the Square Root of a number.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("Now you have %g problems.\n", math.Sqrt(4))
}
Here you have to import only math in order to perform the square root of a number. So we use the method Sqrt and a number as a parameter. Hence we math.Sqrt(4) and we get the result as 2.
Program 5: Write a program in Go lang to find the value of Pi.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Pi)
}
Program 5: Write a program in Go lang to find the value of Pi.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Pi)
}
Tip to all, when you export any function or anything name in Go, make sure that it always begins with a Capital letter. For example in the above program when you change Pi to pi you end up getting this error:
cannot refer to unexported name math.pi.
undefined: math.pi.
undefined: math.pi.
Make sure that you always follow the naming convention.
So, that's all about Module 1 these are pretty much basics to get started with Go lang. I'm currently working on Module 2, which I'll release it shortly. Stay tuned and keep practicing the above Module.
For those of you who are interested in learning more about Go lang, you can visit the official site called Tour of Go where I have taken the code snippets to teach in this blog, So all the credit goes to Google.
Source: Tour of Go
Thank you Guys for spending your time reading my Blog, stay tuned for more updates. Let me know what is your opinion about this tutorial in the comment section below. Also if you have any doubts regarding the code, comment section is all yours. Have a nice day.
Comments
Post a Comment