What is Go Language?
Go language is developed by Google. Go is a compiled language. It is the most promising language.
Docker and Kubernetes are developed by Go language. Technical base is from C and C++.
As Go language is easy to learn, read and write programs, Go language minimalistic.
Advantages of Go language
Easy to read and learn
Easy to write programs
Easier than java because it has less features to focus on.
Lets get started with Go Language
Go to the below site where you can download the setup for Go language.
After downloading, open the setup and install.
To check whether Go is properly installed, open the command prompt and type GO and press enter.
Then you might get a list of commands. If so GO lang is successfully installed.
You can create a file and save with the extension of ".go". And try the below small program.
Then open the command prompt in the location where the file is saved and give the below command and run it.Then you should get an output as belowAlso you can add two numbers inside the print statement as below
Output
In GO language, if you are declaring a variable you must use that declared variable.
If you ignore the declared variable as above example, you may get an error as shown below
Also If you are importing a package you must use that imported package.
If you ignore the imported packages as above example, you may get an error as shown below
But in java whatever the package you import or whatever the variable which you declare, it is not a must
to use it. But in Go, you must use it.But if we missed one thing when declaring the variable. Even though the program is compiled, it is
incomplete. When declaring a variable we need
Variable name
Data type
Data
Example
output
Example
what we missed in above examples is variable type. It should be done like below
outputIf you did not assign a value for a variable, by default its value becomes 0
If you do not assign a value and also if you do not give a type for the variable, it will occure an error.
Output
You can try the below examples aswellThe below example will show you an infinite loop
- For loop
OR
Use of functions
In C language and in JAVA we cannot return multiple values from a function, but in GO language we can return
multiple languages from a function
According to the below example, you can just write the keyword "return" inside the method and specify
the variables that you expect to return from the function when declaring the variable.
If you want to access a variable in both methods do it like below example
Package level and function level variables
In java its global and local variables but in GO its package level and function level
Any function will reffer the package level variable only if it does not have a function level variable
Go language is case sentisitive. The best example for that is when printing a statement you use
fmt.Println("Hello")
But if you use simple 'p' as below it won't work
fmt.println("Hello")
Lets use a math function
Remember Sqrt function accepts only float values. Not accepting integers
Also you can format your output as below
If you want to round your value for 2 decimal points then use %.2f as below
Lets get the integer output , ceil output and the floor output as below
You can try below examples aswel
Comments
Post a Comment