Go variables and functions
Introduction
Go a.ka. golang is a statically-typed compiled high-level programming language as you can find on google yourself. Now let's look at some other aspects of go. Go is a procedural language which means that we define a series of instructions called procedures to create programs. It supports concurrency in the form of go routine which are light-weight threads. It is a modern programming language developed by Ken Thompson, Robe Pike, and Robert Griesemer in 2007 and released in 2009 as an open-source programming language.
Use Cases
Go is a very fast language and it has built-in support for network programming and concurrency. Some of the major use cases of go are the following.
DevOps and Cloud computing: Many of the DevOps tools like docker, Kubernetes, Graphana, Prometheus, etc. are written in Go.
Web Development: Golang is being used to write high-performance backends. Frameworks like fiber and gin can be used to build backends in Go. Go is also widely used to build microservices.
Other than these two, you can do almost anything with Go.
Basics: Variables
Variables are named locations in the computer memory where you can store data. You can access the value using the name of the variable. To create variables in go look at the following code snippet.
// This the main function where the program execution begins
func main(){
// Now I am creating a variable named 'name' of type string
// with value Rishabh
var name string = "Rishabh"
}
So the basic syntax is as follows
// creating a variable without any initial value
var <variable-name> <data-type>
// or
// creating a variable with given value
var <variable-name> <data-type> = <value>
// If value is provided data-type can be omitted
var <variable-name> = <value>
Now there is another way to create variables in Go, check this out
func main(){
// this is another way to create variable
name := "Rishabh"
}
Pay attention that I am not providing any data type information while creating the variable. Go compiler is smart enough to infer the type of variable depending on the value to the right of :=
.
Note you cannot use the second syntax in the global scope, i.e. it is valid only inside functions.
// Invalid statement, since it is outside the function
country := "Wakanda"
// Valid statement, You can use var syntax to create variable
// outside the functions.
var leader string = "Black panther"
func main(){
// valid statement
var number int = 99
// valid statement, allowed inside functions
number2 := 50
}
Note: You cannot use short-hand assignments outside the functions.
Multiple assignments on the same line
You can assign values to multiple variables on the same line, by separating variables by ,(comma)
and providing values in the same order as variables like in the following code.
import "fmt"
func main(){
var a,b,c int = 45,66,99
// a = 45, b = 66, c = 99
fmt.Println(a,b,c)
x,y,z := 10,20,30
// x = 10, y = 20, z = 30
fmt.Println(x,y,z)
}
Functions
The function is a named group of statements that performs some specific task. For writing functions, there are three important parts,
Name of the function
Input of the function
Output of the function.
Function syntax
func <name of function>(<list of arguments(input)>) <return-type> {
<body of the function>
}
// Example of a function
// this function takes two integers, adds them and
// returns an integer i.e. sum
func sum(num1 int, num2 int) int{
return num1+num2
}
If the arguments of a function are of the same type you can omit data types for all but the last argument. Observe the following code
func sum(num1, num2 int) int {
return num1+num2
}
Multiple return values
Any number of values can be returned by a function. You just need to provide the list of return types in the function signature
func twoValues(x, y int)(int,int){
return y,x
}
Notice how I have provided a list of return types after the argument list.
Named return types
You can also name the return types and in this way, there is no need to write the name of the returned value Go will automatically return the named return values.
For now, observe the following code.
func myFunction(a int, b int)(z int){
z = a + b
return
}
In the above code, you can see I haven't returned anything but since I have named the returned variable, Go will automatically return the z.
As per Go documentation, you should use named return values only for short functions.
Conclusion
This is it for this blog I will be discussing data types, type casting and constants in the next one. I am going to share all my learnings, in a way that would help others. Feel free to suggest or discuss programming.
I will be posting more Go content, So stay tuned. More blog posts are coming where I will dive deep into this modern language.
Link to next blog is here.
Like, Comment, Share and Follow for more.
Keep supporting, It motivates me to keep creating content.