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.

Packages

  • 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

name.go
package stringutil
/*My name will be exported because
 it starts with capital letter*/
var MyName = "Software"
var yourname = "Product"
reverseTwo.go
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)
}
reverse.go
package stringutil
//Reverse returns its argument as reversed string
func Reverse(s string) string {
 return reverseTwo(s)
}
main.go
package main
import (
 "fmt"
 "package/stringutil"
)
func main() {
 fmt.Println(stringutil.Reverse(stringutil.MyName))
 //fmt.Println(stringutil.Reverse(stringutil.yourname))
}

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 –

TYPESDESCRIPTION
uintUnsigned 8 bit integers (0 to 255)
uint16Unsigned 8 bit integers (0 to 65535)
uint32Unsigned 32 bit integers (0 to 4294967295)
uint64Unsigned 64 bit integers (0 to 18446744073709551615)
int8Signed 8 bit integers (-128 to 127)
int16Signed 16 bit integers (-32768 to 32767)
int32Signed 32 bit integers (-2147483648 to 2147483647)
int64(-9223372036854775808 to 9223372036854775807)

Floating Types –

TYPESDESCRIPTION
float32IEEE-754 32 bit floating point numbers
float64IEEE-754 64 bit floating point numbers
complex64complex numbers with float32 real and imaginary parts
complex128complex numbers with float64 real and imaginary parts

Other Numeric Types –

TYPESDESCRIPTION
bytesame as uint8
runesame as int32
uint32 or 64 bits
intsame as uint
uintptran unsigned integer to store uninterpreted bits of pointer value

Variables

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?

General

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

%#v → value representation

%T → data type of value

Boolean:

%t → the word true or false

Integer:

%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

String:

%s → String or slice

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

Pointer:

%p →Hex representation with leading 0x

For the formats go to Go documentation – http://godoc.org/fmt

main.go
package main
import (
 "fmt"
)
//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()
 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()
 fmt.Println(info)
 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

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

name.go
package myPack 
var MyName = "Anthony"
your.go
package myPack
var YourName = "Gonsalves"
main.go
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
 fmt.Println(y)
 z := myPack.MyName /*MyName should start with Capital letter
 because we are accessing it from another package*/
 fmt.Println(z)
 a := myPack.YourName
 fmt.Println(a)
 b:=0
 start:=call() //Assigning a func expression to value
 fmt.Println(start)
}
func print(s string) string {
 return s
}
func call() int {
x:=11
x++
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.

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

Error: b declared but not used

main.gopackage main
import (
“fmt”
“io/ioutil”
“net/http”
)
func main() {
res, _ := http.Get(“https://www.google.com/”)
/*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)
res.Body.Close()
fmt.Printf(“%s”, page)
}

Constants

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.

main.go
package main
import (
 "fmt"
)
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)
 fmt.Println(a)
 fmt.Println(b)
 fmt.Println(c)
 fmt.Println(d)
 }()
}

“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.

main.go
package main
import (
 "fmt"
)
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 –

Pointers

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.

k:=50

p=&k

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

package main
import (
 "fmt"
)
func main() {
 a := 50
 fmt.Println(a)
 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 ( 
 "fmt" 
) )
func main() { \
s := 5 
call(s) 
fmt.Println(s) 
} 
func call(n int) { 
 n++ 
} 
//Here o/p is still 5
package main
import ( 
"fmt"
)
func main() {
s := 5
call(&s)
fmt.Println(s) 
}
func call(n *int) {
*n++
}
//Here o/p is will 6 as required
Demo Screenshot –

Remainder

We use “%” for getting remainder.

package main
import (
 "fmt"
)
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’.

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 contact@mindbowser.com

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?