Need help?

GoLang Fundamentals

As we have already covered the basics of Golang in our First Chapter and how to create your first Go Project in Second Chapter, let’s dive deeper into language fundamentals.


  • Organize the code just like we organize our files into computers
  • Folder name should be same as the package name
  • One package many files (If something is in lowercase, it will not be exported outside the package)
  • Package scope: something in one file is accessible to another file
  • Imports have file scope
  • Capitalization:
    Uppercase: exported, visible outside the package
    Lower case: unexported, not visible outside the package

Find below the example of how packages can be created and used

package stringutil
/*My name will be exported because
 it starts with capital letter*/
var MyName = "Software"
var yourname = "Product"
package stringutil
func reverseTwo(s string) string {
 r := []rune(s)
 for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
 r[i], r[j] = r[j], r[i]
 return string(r)
package stringutil
//Reverse returns its argument as reversed string
func Reverse(s string) string {
 return reverseTwo(s)
package main
import (
func main() {

Here, if you try to access the stringutil. yourname variable it will give an error like below – (because yourname variable should start with capital letter)

Data Types

Data types are used for declaring variables or functions of different types. The variable type determines how much memory space occupies in storage and bit pattern stored is interpreted.

  • Boolean Types – These are predefined constants : (a)true (b) false
  • Numeric Types – Represents (a) integer types (b) Floating point values
  • String type – Predeclared string type is string having set of characters
  • Derived types – (a)Pointer types (b)Array types (c) Structure types (d)Union types (e)Function types (f) Slice types (g)interface types (h) Map types

Integer Types –

uint Unsigned 8 bit integers (0 to 255)
uint16 Unsigned 8 bit integers (0 to 65535)
uint32 Unsigned 32 bit integers (0 to 4294967295)
uint64 Unsigned 64 bit integers (0 to 18446744073709551615)
int8 Signed 8 bit integers (-128 to 127)
int16 Signed 16 bit integers (-32768 to 32767)
int32 Signed 32 bit integers (-2147483648 to 2147483647)
int64 (-9223372036854775808 to 9223372036854775807)

Floating Types –

float32 IEEE-754 32 bit floating point numbers
float64 IEEE-754 64 bit floating point numbers
complex64 complex numbers with float32 real and imaginary parts
complex128 complex numbers with float64 real and imaginary parts

Other Numeric Types –

byte same as uint8
rune same as int32
uint 32 or 64 bits
int same as uint
uintptr an unsigned integer to store uninterpreted bits of pointer value


There are two primary ways of declaring variables –

shorthand method → can only be used inside func (mostly used)

var → used to set things to zero values

Here no value is initialized and later assigned to variables. Go automatically assigned default values to it

For Booleans→ false for integer→ 0 for float→ 0.0 for strings→ ””

For pointers, maps, slices, functions, interfaces, channels→ nil

Are You Looking For Golang Development Services?


%v → value in default format, when printing structs use +%v

%#v → value representation

%T → data type of value


%t → the word true or false


%b → binary representation of int value

%d →decimal representation of int value

%c → character representation

%o → octal representation of int value

%x →Hex representation with small letters a-f

%X → Hex representation with capital letters A-F


%s → String or slice

%q →double quoted string safely escaped with Go-syntax


%p →Hex representation with leading 0x

For the formats go to Go documentation –

package main
import (
//Default Values of Variables (Package Scope)
var h int
var i string
var j float64
var k bool
func main() {
 //Shorthand Notations
 a := 34
 b := "Learning Golang"
 c := 5.23
 d := true
 e := "Welcome"
 f := `How are you?`
 g := 'S'
 //Variable declaration
 var info string
 info = "Welcome to Golang"
 //Declare many at once
 var l, m, n string //same type
 l = "Google"
 //Initialize many at once
 var o, p int = 32, 54
 //Mixed Types
 var q, r, s = true, 433, "Hello"
 t, u, v := false, 54.56, 'H'
 w := `Hello` //Backtricks considered as string

 fmt.Printf("Type:%T \t Value:%v \n", a, a)
 fmt.Printf("Type:%T \t Value:%v \n", b, b)
 fmt.Printf("Type:%T \t Value:%v \n", c, c)
 fmt.Printf("Type:%T \t Value:%v \n", d, d)
 fmt.Printf("Type:%T \t Value:%v \n", e, e)
 fmt.Printf("Type:%T \t Value:%v \n", f, f)
 fmt.Printf("Type:%T \t Value:%v \n", g, g)
 fmt.Println("Default Values---")
 fmt.Println("int:", h)
 fmt.Println("string:", i) //string has empty value
 fmt.Println("float64:", j)
 fmt.Println("bool:", k)
 fmt.Println("l,m,n:", l, m, n)
 fmt.Println("o,p:", o, p)
 fmt.Println("q,r,s:", q, r, s)
 fmt.Println("t,u,v:", t, u, v)
 fmt.Println("backtricks w:", w)


Scope means where the variable will be accessible. We declare variables, functions, identifiers in program.

Levels Of Scope :

universe → package → file → block (curly braces)

{} – braces []- brackets ()-parentheses

Package Level Scope→ for variables ; not for imports

The scope of x is for the whole package; means it is accessible throughout the program. But if we declare any variable inside the function then we can’t use it in another function.

In the below examples, capitalized variables having package scope can be imported to another package

package myPack 
var MyName = "Anthony"
package myPack
var YourName = "Gonsalves"
package main
import (
 "scope/scopes/myPack" "fmt"
var x string = "Welcome to Golang" //Accessed in whole program
func main() {
 y := print(x) //y cannot be accessed outside this function
 z := myPack.MyName /*MyName should start with Capital letter
 because we are accessing it from another package*/
 a := myPack.YourName
 start:=call() //Assigning a func expression to value
func print(s string) string {
 return s
func call() int {
return x

Demo Screenshot –

Here name.go and your.go files are present in “scope/scopes/myPack”” package. Importing of the package is needed to use

variables/functions from another package.

Closure { } limits the scope of the variable outside the functions

Access scope matters a lot. The imports pFile Level Scope → It is only for packages

Block Level Scope → access scope is only within the function itself

Remember, keep your scope tight!

Blank Identifier

Keep in mind that you must use everything that you put in your code

If you declare a variable, you must have to use it otherwise compile time error will show. The blank identifier tell compiler that you are not using something and it is declared with ‘_’ symbol.

package main
import (
func main() {
 a := "Software Development"
 b := "Production Planning"
 //b is declared but not used so it's invalid code

Error: b declared but not used

main.gopackage main
import (
func main() {
res, _ := http.Get(“”)
/*Actually here,http.Get() returns resp and error but Here it is saying that I don’t want to use the error variable by specifying ‘_’ */
page, _ := ioutil.ReadAll(res.Body)
fmt.Printf(“%s”, page)


In Golang “const” keyword is used for declaring constants. These constants value is like final that no one is allowed to change and will remain the same whenever we are using it. It is an unchanging value.

package main
import (
type Byte float64
//Single Value Initialization
const pi = 3.14

//Multi Value Initialization
const (
 userName = "product"
 password = "product@123" //String constant
 a = iota //2
 b = iota //3
 c = 1 << iota //16 (iota=4)
 d float64 = iota * 5 //25 iota val inc by 1
func main() {
 func() {
 rad := pi * 7 * 7
 fmt.Println("Radius of Circle:", rad)
 fmt.Println("UserName:", userName)
 fmt.Println("Password", password)

“Iota” is used in const declaration for simplifying declarations of incrementing variables. The value of iota is reset to 0 and increments after each line.

Memory Addresses

For every variable value, we stored has memory address. To know the stored memory address of variable we use ‘&’ (ampersand) operator.

package main
import (
const pi = 3.14
var message string = "Product"
func main() {
 responseCode := 101
 fmt.Printf("\n message:%v,Address:%v", message, &message)
 fmt.Printf("\n responseCode:%v,Address:%v", responseCode, &responseCode)
 area := pi * 7 * 7
 fmt.Printf("\n Area:%v,Address:%v", area, &area)

Demo Screenshot –


Like C Language, Go also supports pointer. A pointer is used to hold the memory address of a value. It is represented by using “*” (asterisk) just like the C language.

For example, *int is pronounced as pointer to int value.

var p *int

The & operator generates a pointer to its operands.



fmt.Println(*p) //Here, we are getting a value of p by pointing to it

package main
import (
func main() {
 a := 50
 fmt.Println("a:", &a) //Address of a
 /*Here x will point to the address where a
 is stored */
 var x = &a //Referencing
 /*Here x will get the value by using '*' */
 fmt.Println("x:", *x) //DeReferencing
We can pass memory address i.e. reference instead of a bunch of values and we still can change the values. So we don’t need to pass values, instead, we can pass addresses.

Problem Without Pointer

package main 
import ( 
) )
func main() { \
s := 5 
func call(n int) { 
//Here o/p is still 5
package main
import ( 
func main() {
s := 5
func call(n *int) {
//Here o/p is will 6 as required
Demo Screenshot –


We use “%” for getting remainder.

package main
import (
func main() {
 for i := 1; i < 100; i++ {
 if i%2 == 1 {
 fmt.Println(i, "is Odd number")
 } else {
 fmt.Println(i, "is Even number")

This is all about language fundamentals. Check out chapter 4 where we will talk about ‘Control Flow’ and ‘Functions’.

Subscribe To Our Newsletter

Content Team

This blog is from Mindbowser‘s content team – a group of individuals coming together to create pieces that you may like. If you have feedback, please drop us a message on
We do publish often and our blogs are often accompanied by research and surveys. If you would like to be the first to receive an update on whenever we publish, subscribe to our newsletter.

Get in touch for a detailed discussion.

What’s on your mind? Tell us a little bit about yourself and your question, and we will be in touch with you within 12 hours

Free eBook on Telemedicine Platform Development: All About Telemedicine

Download Free eBook Now!