DEV Community

Cover image for Go Language to go for beginners
karthik-ke
karthik-ke

Posted on

Go Language to go for beginners

#go

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

Sample code:

Alt text of image

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

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

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

3.Traditional pattern:

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

Alt text of image

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

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

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

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

Switch:

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

Alt text of image

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

:) :) :)

Top comments (0)