πŸ‘¨πŸ’»
Go

Functions are their own data type in go
  • Functions can be assigned to variables
  • Functions can be passed as paramters

func functionName(param1, param2 int) {}
// Funtion call
functionName(param1, param2)

func functionName() (int, string) {
return 22, "Maheshrjl"
}
// Function Call
var x, str = functionName()

func functionName(param1, param2 int) int{}
​
//Function Call
returnVariable := functionName(param1, param2)

func add(x,y int) (int, int){return x,y}
//Function call
varx, vary := add(1,1)

func add(x,y int) (variablex int, variabley int){
variablex = x
vairabley = y
return
}
//Function call
varx, vary := add(1,1)

func add(x,y int) (variablex int, variabley int){
defer fmt.Println("Adding x + y") // Execute this at function return
variablex = x
vairabley = y
return
}

func test(){
fmt.Println("Hello!")
}
​
func main(){
x := test
x() // This calls the test function
}

func main(){
test := func() {//Test is a variable storing a function
fmt.Println("Hello!")
}
test()
}

// Work in Progress

A closure is a function value that references variables from outside its body
// Work in Progress

An array type defiiniation specifies a length and an element type. Eg: 4[int] represents an array of 4 integers.
  • Size of an array is fixed and it's length is a part of it's type ([4]int and [5]int are distinct, incompatible types.
  • Arrays do not need to be initalized explicitly, an unassigned array will have a 0 value.

name := [2]string{"Mahesh","Rijal"}
or
name := [...]string{"Mahesh","Rijal"}

Array has a fixed size. But, slices are dynamically sized. Slices support append and copy functions

s := make([]string, 3)
The make function allocates a zeroed array and returns a slice that refers to that array

A struct is a sequence of named elements, called fields, each of which has a name & type.
type Rectangle struct{
Width float64
Height float64
}
Last modified 1mo ago
Copy link
Edit on GitHub
On this page
Functions
Data Structures
Array
Slices
Struct