Golang Language Fundamentals Chapter 3
by- Kabeer Shaikh
Originally published at RemotePanda Blogs
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
- Lowercase: unexported, not visible outside the package
Demo Screenshot
Find below the example of how packages can be created and used
Here, if you try to access the stringutil.yourname variable it will give an error like below — (because your name variable should start with a capital letter)
Demo Screenshot
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 the string having set of characters
- Derived types — (a)Pointer types (b)Array types © Structure types (d)Union types (e)Function types (f) Slice types (g)interface types (h) Map types
Integer Types
Types
uint
uint16
uint32
uint64
int8
int16
int32
int64
Description
Unsigned 8-bit integers (0 to 255)
Unsigned 8-bit integers (0 to 65535)
Unsigned 32-bit integers (0 to 4294967295)
Unsigned 64-bit integers (0 to 18446744073709551615)
Signed 8-bit integers (-128 to 127)
Signed 16 bit integers (-32768 to 32767)
Signed 32 bit integers (-2147483648 to 2147483647)
(-9223372036854775808 to 9223372036854775807)
Floating type
Types
float32
float64
complex64
complex128
Description
IEEE-754 32-bit floating point numbers
IEEE-754 64-bit floating point numbers
complex numbers with float32 real and imaginary parts
complex numbers with float64 real and imaginary parts
Other Numeric types
Types
byte
rune
uint
int
uintptr
Description
same as uint8
same as int32
32 or 64 bits
same as uint
an 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
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
Scope
Scope means where the variable will be accessible. We declare variables, functions, identifiers in the 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 below examples, capitalized variables having package scope can be imported to another package
Demo Screenshot
Here name.go and your.go files are present in “scope/scopes/myPack”” package. Importing of 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 tells the compiler that you are not using something and it is declared with ‘_’ symbol.
Error: b declared but not used
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 remains same whenever we are using it. It is unchanging value.
“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.
Pointers
Like C Language, Go also supports pointer. 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
We can pass memory address i.e. reference instead of 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
Demo Screenshot
Remainder
We use “%” for getting remainder.
This is all about language fundamentals. Check out chapter 4 where we will talk about ‘Control Flow’ and ‘Functions’.
About Kabeer Shaikh:
Kabeer Shaikh is a Android Developer At MindBowser Info Solutions
About RemotePanda:
RemotePanda is a personalized platform for companies to hire remote talent and get the quality work delivered from our city Pune. All talents associated with us are close network connections. When we connect them with you, we make sure to manage their quality, their growth, the legalities and also the delivery of work. The idea is to make remote work successful for you.
Stay connected with us on Facebook, Twitter, LinkedIn and YouTube