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.
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 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.
TYPES | DESCRIPTION |
---|---|
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) |
TYPES | DESCRIPTION |
---|---|
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 |
TYPES | DESCRIPTION |
---|---|
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
%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 – 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 means where the variable will be accessible. We declare variables, functions, identifiers in program.
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!
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)
}
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.
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 –
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 }
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
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’.
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!