So for the first article of Go with Golang got superb feedback and with that today we’re going to start writing some complex programs than simple hello world programs.
Last time, there were some problems like how many libraries Go have, are there any specific place to save Go and so many. So now you know about the package main, the first statement of a Go program and then we are adding our libraries to do some functions in our program. You can get these programs by https://golang.org/pkg/.
There are some common packages we using in our programming, like “fmt” which control I/O operations printf and scanf, “math” which using for mathematical functions, “net” that we using in network interfaces and so on. Last practical session we used only one library, “fmt”. So I will show you how we can use more than one library in our program.
import (“fmt” “math”) – consider that there are no any comas or semi colons in this statement. In this statement I’ve used two main libraries, fmt and math.
Sometimes you may have some problems of keywords, how we can use correct keywords with the imported libraries. Lets use one keyword Printf, you can go to https://golang.org/pkg/fmt/ the correct imported library name.
Last article we run an Go file using go run keywords and we can do the build by go build. But this will not give any kind of output. But it will give us an executable file. You can see the build process, and the executable file in below images.
Build the application
After the build, There is a executable file called Golang.exe
If you go to this executable file, you can see the program correctly.
Variables of GO
So after this short explanation, I will discuss about the variables of Go. When u defining variable, you have to add var variable_name variable_type.
Examples: var age int, var name string64
Let’s create a programs using these variables.
Go programs with three variables
You can see the above program, which written using Go. It has three main variables, a,b and c. These three variables are integer type variables. We already import the fmt, so we will use fmt to print the variable c.
Go language has a shorthand version. It will remove some parts like, var and data type. You can see the shorthand version in below program.
From next article, lets talk about arrays of Go, and also about For loops use in Go.