Golang
Go language is an open-source, statically typed programming language by Google. Go is highly recommended for creation of highly scalable and available web applications.
Basics
Sample Go program
package -- package declaration which is mandatory for all Go programs. import "fmt" -- used to import built-in fmt package. func main() -- function is the starting point of execution of any Go program. fmt.printf -- inbuilt library function which is used to print the given message. fmt.Scanf -- inbuilt library function which is used to read the data. // -- to comment a single line /**/ -- to comment multiple lines
Variables
Declaring Variables
var variable-name data-type Assigning value to variables variable-name := value // It's not mandatory to declare a variable when you use this. Example
Data types
1. Numeric Data types
Integer Data types
Data type | Description | Size | Range | Alias |
---|---|---|---|---|
uint8 | 8-bit unsigned integer | 1 byte | 0 to 255 | byte |
int8 | 8-bit signed integer | 1 byte | -128 to 127 | N/A |
int16 | 16-bit signed integer | 2 bytes | -32768 to 32767 | N/A |
unit16 | 16-bit unsigned integer | 2 bytes | 0 to 65,535 | N/A |
int32 | 32-bit signed integer | 4 bytes | -2,147,483,648 to 2,147,483,647 | int (on 32-bit systems), rune |
uint32 | 32-bit unsigned integer | 4 bytes | 0 to 4,294,967,295 | uint (on 32-bit systems) |
int64 | 64-bit signed integer | 8 bytes | -9e18 to 9e18 | int (on 64-bit systems) |
uint64 | 64-bit unsigned integer | 8 bytes | 0 to 18e18 | uint (on 64-bit systems) |
Float Data types
Data type | Description | Size | Alias |
---|---|---|---|
float32 | 32-bit signed floating point number | 4 bytes | N/A |
float64 | 64-bit signed floating point number | 8 bytes | float |
complex64 | float32 real and imaginary | 8 bytes | N/A |
complex128 | float64 real and imaginary | 16 bytes | N/A |
2. Boolean
bool — 1 byte — true or false
3. String
string — sequence of characters rune — alias for uint32, represents single char
Example
Operators
Arithmetic: +, -, *, /, % Comparison: <, >, <=, >=, !=, == Bitwise: &, ^, |, &^, <<, >> Logical: &&, ||, ! Assignment: =, +=, -=, *=, etc.
String Functions
len(str), strings.Compare(a, b), strings.Contains(str, substr), strings.ToUpper(str), strings.ToLower(str), strings.HasPrefix(str,"prefix"), strings.HasSuffix(str,"suffix"), strings.Index(str, substr), strings.Join(string, sep), strings.Count(str, sep)
Arrays
Conditional Statements
if, if-else, else-if ladder, switch
Loops
for, for as while, infinite for + break
Functions
Pointers
Structures
Slice
Maps
Concurrency
- Go-routines
- Channels
Generics (Go 1.18+)
Using interface
Want to update?
Subscribe to my blog to receive the latest updates from us.