Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - BASIC

Go

  1. Go was developed by Google in the year 2007 by Rob Pike, Robert Griesemer, and Ken Thompson.

  2. Go is fast due to its fast compilation.

  3. Go is concurrent by default.

  4. Go is easy to read and write.

  5. Successful implementation of Interfaces makes Go a step ahead of others.

  6. Go has a Garbage collector that prevents from large errors by preventing memory leak.

Why would you choose Go ?


Web Services :  Due to the built in Concurrency feature of Go, it is mostly preferred because because Web Services deals with multiple transaction. And Go's Concurrency model helps to achieve the same.


Automation :  Although automation is good for scripting languages like Go. But Go is a suitable in this case as well.


Easy to Read/Write :  As said earlier, Go is quite easy to read and write, as code written in Go is quite similar to plain English and Mathematics.


First Go Application


Example :


package main
import "fmt"
    
func main() {
    
    fmt.Println("Hello World")
}


Output :



  Hello World

Note : We have a separate tutorial on 'Functions'. For now, you do not have to dive more into 'Functions'.

For now, you can least bother about the first two lines, we will understand in the coming tutorials.


package main
import "fmt"

Just remember that the above lines are the mandatory lines that is needed to run a Go application.

  1. So, in the above code we are printing 'Hello World' using the below statement,

    fmt.Println("Hello World")

    We can just remember, 'fmt.Println( )' is the statement used to print something on the screen.

  2. All Go programmes should be written inside the Function called 'main( )'. Where, 'func' is the short form of 'Function'.

    func main() {
    			
        ...
    }   

  3. The main( ) Function is the entry point of a Go application. So, Go executes the code written inside the main( ) method :

    func main() {
    
        fmt.Println("Hello World")
    }

Since, 'fmt.Println("Hello World")' is inside the main( ) method, it got printed.


Moral of the Story


We have to write all our codes inside the main method. And the main method should be inside a class.


Example :


package main

import "fmt"
    
func main() {
    
    //OUR CODE SHOULD BE WRITTEN HERE.
}


Where do we write Go related code?


The codes in Go should be written inside a file with extension '.go'.


So, we will be writing the above code to print 'Hello World' inside a file named 'FirstApplication.go'.


FirstApplication.go


Example :


package main
import "fmt"
    
func main() {
    
    fmt.Println("Hello World")
}    



Now, to execute the above code, we have to type the following in the command prompt :


go run FirstApplication.go


Output :



  Hello World