Golang Language Fundamentals

Golang Language Fundamentals

golang language fundamentals

 

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

Packages

  • Organize the code just like we organize our files into computers
  • Folder name should be the 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
Go

 

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 (c) 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

 

Golang project explorer

Demo Screenshot

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

Golang Package

 

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)

Golang
Golang 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

Golang
Golang Demo

 

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 the otherwise compile-time error will show. The blank identifier tells the compiler that you are not using something and it is declared with ‘_’ symbol.

Go

 

Error: b declared but not used

go

 

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.

Constant

 

“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 a memory address. To know the stored memory address of variable we use ‘&’ (ampersand) operator.  

Go

 

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

Pointer

 

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                                                                               

Pointer

 

Remainder

We use “%” for getting remainder.

Remainder

Conclusion

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

Comments are closed.