Go Language to go for beginners 
Tags: go
Published: 2020-12-17 10:02:07

__Overview__: 

Go language is designed & developed at Google (Go). It is a compiled language that is like C and CPP but with memory safety, structural typing, and CSP (Communicating Sequential Processes). 
This documentation will not guide you to each element in Go but will give you a good idea to start with Go. Go’s key feature is concurrency.

**_Some software developed using Go_**:
* Docker
* Kubernetes
* Heroku 

__Go - Hello world:__

We had traditional to practice any code, we start by writing a hello world sample code. We go by the same, 

For installation and get started: [Go](https://golang.org)

**_Sample code:_**

![Alt text of image](https://raw.githubusercontent.com/karthik-ke/common_images/main/go%20sample%20code%20from%20go%20site.png)

As I referred earlier Go influenced by C, Go code start executed from the main function. 

Unlike C programming language if you want to use any in-built function you need to specify the package name before the function, here the Println function comes from `fmt` (format) package. The function definition is identified in Go with the keyword func.

All the functions and codes in Go should be under a package so that we can use the current package in another package (same as fmt in the above package-main). 

> Function open curly bracket should start with the same line of the function definition

__Variables:__

Variable creation and assigning value in Go in different ways as follows,

***1. Declaring a variable:***
   `var a int`
   * Variable `a` created here without assigning value to it, the default value of a is 0 (int).

***2. Declaring a variable and assigning value:***
   `var a= 5`
   * `var` is the keyword which is declaring variable `a`
   * `5` is the value for variable `a`
   `var a int = 5`
   * In the above example same, we are assigning value 5 to the variable `a` but mentioning its type `int`. Here specifying type is optional, but when it comes to declaration alone it's mandatory.
 
***3. Declaring a variable and assigning value:***
   `a := 5`
   * Here creation and assigning a value for `a` is happening by using the operator `:=`. 

![Alt text of image](https://raw.githubusercontent.com/karthik-ke/common_images/main/go_variable_assign.jpg)

**_Variables available in Go_**:

*	Bool 
*	String 
*	int, int8, int16, int32, int64 
*	uint, uint8, uint16, uint32, uint64
*	float32, float64 

> Creating a variable or import any package and not using anywhere in your Go throws error unlike other languages (Python, Java, C, CPP).

__Loops:__

Go have only `for` loop unlike other languages Go does not have `do while`, `while` loops in it. When it comes to loops, we need to specify three things variable value assignment, condition, incrementation. 

***1. Infinite loop:*** 

In this type, we do not specify any of the three-parameter we mentioned above, so it becomes an infinite loop, useless though I want to mention this weird functionality availability.

![Alt text of image](https://raw.githubusercontent.com/karthik-ke/common_images/main/infinite_loop.jpg)

***2.Variable assignment, condition, increment/decrement in different areas:***

Go is not like C language always specify assignment, condition, increment in one place, Go allows these three in different places as well. 

![Alt text of image](https://raw.githubusercontent.com/karthik-ke/common_images/main/for_1.jpg)

***3.Traditional pattern:***

Assignment, condition, and increment/ decrement at loop initialization.

![Alt text of image](https://raw.githubusercontent.com/karthik-ke/common_images/main/for_2.jpg)

__Functions:__

Go language function declaration is done by using the key word `func`.
***1.	Function with no return value:*** 
We can able to create a function without any return. Just to mention function without return type is possible in Go.

![Alt text of image](https://raw.githubusercontent.com/karthik-ke/common_images/main/go_func_no_return.jpg)

***2.	Function with multiple parameters & one return:***

In the below snippet add function takes two-parameter and adding and returns a single value. Unlike other languages like python, we need to specify the type of return here in Go.

![Alt text of image](https://raw.githubusercontent.com/karthik-ke/common_images/main/go_fun_1_return.jpg)

As we discussed earlier variable declaration and assignment happening at `res: = add (2,3)`. 

***3.	Function with multiple parameters & multiple return:***

Some languages do not support multiple return values but Go gives that feature. Refer to the below snippet. 

![Alt text of image](https://raw.githubusercontent.com/karthik-ke/common_images/main/go_fun_2_return.jpg)

The above add function has two input values a, b as integer and `sum_val`, `sub_val` as output, and their return also mentioned. In the function we can just give a return it will work in Go. 

>If you want to use your own function in a package outside of the other package you need to create that function with a capital letter if you are creating a function with the small case you can’t use that outside of the package. 
func `inside_package ()` --> only accessible inside a package 
func `Outside_package ()` --> accessible by other packages as well. 

__Conditional statements in Go:__

Go has two conditional statements if-else and switch. It is typically the same as C will be seeing the example so that we can have a better understanding. 

***if else:***

One thing to be noted herein Go we start else if or else block and end of the if block itself. 

![Alt text of image](https://raw.githubusercontent.com/karthik-ke/common_images/main/if_else.jpg)

***Switch:***

Same as C switch comes with default when condition not falling in any of the cases. 

![Alt text of image](https://raw.githubusercontent.com/karthik-ke/common_images/main/switch.jpg)

*That all I feel basics to start with Go… Thanks for your time... Thoughts!? Please comment It* 

__:) :) :)__