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’.
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowThe Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork