Hello, I'm Ganesh. I'm working on FreeDevTools online, currently building a single platform for all development tools, cheat codes, and TL;DRs β a free, open-source hub where developers can quickly find and use tools without the hassle of searching the internet.
In my previous post, we learned about the standard library and function calling in Go.
Now, let's understand how function variables are declared and used.
Understanding Proper Function Import
Let's take a simple example of random number generation. To understand this, look at the code below:
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("Random number between 0 to 10: ", rand.Intn(10))
}
In this example, we are importing the math/rand module.
The main part of the import is the suffix, i.e., rand.
rand.Intn(10) is a function call to get a random number between 0 and 10.
By running the code, we can see a random number generated:
gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
Random number between 0 to 10: 4
gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
Random number between 0 to 10: 5
gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
Random number between 0 to 10: 8
gk@jarvis:~/exp/code/rd/go-exmaple$
Now, let's import the Pi variable from the math package.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("Pi value ", math.Pi)
}
Here, we can see that we are importing Pi, where the first letter is capitalized.
Why is this capitalized?
Go uses encapsulation to hide the implementation details of a package.
So, any variable or function starting with a capital letter is public (exported) and can be accessed from outside the package. Conversely, any variable or function starting with a lowercase letter is private (unexported) and can be accessed only within the package.
Function Calling
package main
import "fmt"
func add(a int , b int) int {
return a + b
}
func main() {
fmt.Println("Sum of 2 and 3 is ", add(2, 3))
}
Any function is defined with the func keyword.
All private functions start with a lowercase letter. In our case, we defined the add function, which starts with a lowercase letter.
We should define the input type and return type: func add(a int , b int) int.
When the function is called with add(2,3), it will return 2 + 3 = 5.
For multiple parameters, we can define it like func add(a int , b int, c int) int.
For multiple return types, we can define it like func add(a int , b int) (int, int).
Variable Declaration
There are 3 types of variable declaration in Go:
- The Standard Declaration
- Type Inference
- Short Variable Declaration
1. The Standard Declaration
This is usually done outside the main function under the main package.
// Declares 'age' as an int. Since no value is given, it defaults to 0.
var age int
// Declares 'name' and assigns a value immediately.
var name string = "Jarvis"
Variables declared without a value are initialized to their zero value (e.g., 0 for integers).
2. Type Inference
This declaration is based on the data assigned and uses the var keyword.
// Go knows this is a bool because of 'true'
var isStudent = true
// Go knows this is a float64
var temperature = 26.5
3. Short Variable Declaration
This is usually used inside the main function. It is similar to type declaration but more concise.
x := 10
Conclusion
We learned about function calling and variable declaration in Go.
Iβve been building for FreeDevTools.
A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction when searching for tools and materials.
Any feedback or contributions are welcome!
Itβs online, open-source, and ready for anyone to use.
π Check it out: FreeDevTools
β Star it on GitHub: freedevtools

Top comments (0)