This cheat sheet provided basic syntax and methods to help you using Golang. Go is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. Go is syntactically similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency. The language is often referred to as “Golang” because of its domain name, golang.org, but the proper name is Go.
They are boolean types and consists of the two predefined constants: (a) true (b) false
Numeric types
They are again arithmetic types and they represent a) integer types or b) floating-point values throughout the program.
String types
A string type represents the set of string values. Its value is a sequence of bytes. Strings are immutable types that are once created, it is not possible to change the contents of a string. The predeclared string type is a string.
Derived types
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types f) Slice types g) Interface types h) Map types i) Channel Types
Integer Types
uint8
Unsigned 8-bit integers (0 to 255)
uint16
Unsigned 16-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
Signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
Floating-Point Types
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
Hello Simplecheatsheet
Happy 3.14 Day
Go rules? true
Integer Constant
some examples of Integer Constant:
85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
212 /* Legal */
215u /* Legal */
0xFeeL /* Legal */
078 /* Illegal: 8 is not an octal digit */
032UU /* Illegal: cannot repeat a suffix */
Floating Type Constant
Following are the examples of Floating type constant:
3.14159 /* Legal */
314159E-5L /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */
String Literals
Syntax
type _string struct {
elements *byte // underlying bytes
len int // number of bytes
}
Some examples of string literals:
"hello, simplecheatsheet"
"hello, \
simplecheatsheet"
"hello, " "simple" "cheatsheet"
The const Keyword
const variable type = value;
Example:
package main
import "fmt"
func main() {
const LENGTH int = 4
const WIDTH int = 5
var area int
area = LENGTH * WIDTH
fmt.Printf("value of area : %d", area)
}
func function_name( [parameter list] ) [return_types]
{
body of the function
}
The declaration of the function contains:
func: It is a keyword in Go language, which is used to create a function.
function_name: It is the name of the function.
Parameter-list: It contains the name and the type of function parameters.
Return_type: It is optional and it contains the types of values that function returns. If you are using return_type in your function, then it is necessary to use a return statement in your function.
Function Arguments
Call by value
By default, Go programming language uses call by value method to pass arguments. In general, this means that code within a function cannot alter the arguments used to call the function
func swap(int x, int y) int {
var temp int
temp = x /* save the value of x */
x = y /* put y into x */
y = temp /* put temp into y */
return temp;
}
Call by reference
The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call.
func swap(x *int, y *int) {
var temp int
temp = *x /* save the value at address x */
*x = *y /* put y into x */
*y = temp /* put temp into y */
}
switch optstatement; optexpression{
case expression1: Statement..
case expression2: Statement..
...
default: Statement..
}
Example:
package main
import "fmt"
func main() {
switch color:=3; color{
case 1:
fmt.Println("Red")
case 2:
fmt.Println("Blue")
case 3:
fmt.Println("Black")
case 4:
fmt.Println("Pink")
case 5:
fmt.Println("Orange")
default:
fmt.Println("Invalid")
}
}
=> Output
Black
Type Switch
Syntax:
switch optstatement; typeswitchexpression{
case typelist 1: Statement..
case typelist 2: Statement..
...
default: Statement..
}
Example:
package main
import "fmt"
func main() {
var value interface{}
switch q:= value.(type) {
case bool:
fmt.Println("value is of boolean type")
case float64:
fmt.Println("value is of float64 type")
case int:
fmt.Println("value is of int type")
default:
fmt.Printf("value is of type: %T", q)
}
}
Consider the following example, which will print the address of the variables defined
package main
import "fmt"
func main() {
i, j := 42, 2701
p := &i // point to I
fmt.Println(*p) // read i through the pointer
*p = 21 // set i through the pointer
fmt.Println(i) // see the new value of I
p = &j // point to j
*p = *p / 37 // divide j through the pointer
fmt.Println(j) // see the new value of j
}
=> Ouput:
42
21
73
Declaring a pointer:
var var_name *var-type
Example:
var s *string /* pointer to a string */
Initialization of Pointer:
// normal variable declaration
var a = 45
// Initialization of pointer s with
// memory address of variable a
var s *int = &a
Nil Pointers:
The nil pointer is a constant with a value of zero defined in several standard libraries.
Example:
package main
import "fmt"
func main() {
// taking a pointer
var s *int
// displaying the resultfmt.Printf("s = ", s)
}
[]T
or
[]T{}
or
[]T{value1, value2, value3, ...value n}
Here, T is the type of the elements. For example:
var my_slice[]int
Components of Slice
Three components of a slice:
Pointer: The pointer is used to points to the first element of the array that is accessible through the slice. Here, it is not necessary that the pointed element is the first element of the array.
Length: The length is the total number of elements present in the array.
Capacity: The capacity represents the maximum size up to which it can expand.
/* declare a variable, by default map will be nil*/ var map_variable map[key_data_type]value_data_type
/* create a map as nil map can not be assigned any value*/
map_variable = make(map[key_data_type]value_data_type)
delete() Function
/* delete an entry */
delete(map_variable,value_data);