Skip to main content
Post Made Community Wiki by Dennis
added 843 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20

Length 14

var x [9]int32

This is an example of how to create an fixed array. You can access it use normal indexing operation x[8] for example. You can iterate any array or slice using for-range statement, for example:

for index, value := range x { // value == x[index]
}
for index := range x {
}
for _, value := range x {
}

Length 13

var x []int32

This is a longer example of slice, slice is a struct that stores current length, capacity and pointer to an array. to get the capacity you can use cap(x), to get the current length of the slice, you can use len(x) function. To add an element to the last element you can use x = append(x,data) function, if capacity equal to the current length, it will copied to another array, and the slice points to that new array.

Length 12

Length 12

Length 14

var x [9]int32

This is an example of how to create an fixed array. You can access it use normal indexing operation x[8] for example. You can iterate any array or slice using for-range statement, for example:

for index, value := range x { // value == x[index]
}
for index := range x {
}
for _, value := range x {
}

Length 13

var x []int32

This is a longer example of slice, slice is a struct that stores current length, capacity and pointer to an array. to get the capacity you can use cap(x), to get the current length of the slice, you can use len(x) function. To add an element to the last element you can use x = append(x,data) function, if capacity equal to the current length, it will copied to another array, and the slice points to that new array.

Length 12

added 1606 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20

Length 12

var x *int32

This is an example of pointer variable, a pointer most useful to change a value that declared outside function, for example:

func SquareIt(n *int32) {
  v := *n
  *n = v * v
}

This function would multiply a variable passed into it (you must prefix with & when passing non-pointer into pointers), for example:

y := int32(12)
SquareIt(&y)
fmt.Println(y) // 144

A pointer also can be used to change function owner's member, for example:

type Person struct {
   Age int
}
func (p Person) Aging1() { p.Age += 1 } // doesn't change the age
func (p *Person) Aging2() { p.Age += 1 } // change the age
func main() {
  me := Person{Age:28}
  me.Aging1() 
  fmt.Println(me.Age) // 28
  me.Aging2() 
  fmt.Println(me.Age) // 29
}

Length 11

That snippet above means that Stringer is a type that could hold anything that already implements String() string function (function named String that returns a string), for exampleexample:

A function can have its own owner, this function often called method in another language, to set the ownership of a function, just type the owner between func and the function name, for example

type Person struct {
  Name string
  Age  int
}
func (p Person) Print() {
  fmt.Println("I'm a person named " + p.Name)
}
func main() {
  me := Person{"kis",28}
  me.Print()
}

Length 11

That snippet above means that Stringer is a type that could hold anything that already implements String() string function (function named String that returns a string), for example:

A function can have its own owner, this function often called method in another language, to set the ownership of a function, just type the owner between func and function name, for example

type Person struct {
  Name string
  Age  int
}
func (p Person) Print() {
  fmt.Println("I'm a person named " + p.Name)
}

Length 12

var x *int32

This is an example of pointer variable, a pointer most useful to change a value that declared outside function, for example:

func SquareIt(n *int32) {
  v := *n
  *n = v * v
}

This function would multiply a variable passed into it (you must prefix with & when passing non-pointer into pointers), for example:

y := int32(12)
SquareIt(&y)
fmt.Println(y) // 144

A pointer also can be used to change function owner's member, for example:

type Person struct {
   Age int
}
func (p Person) Aging1() { p.Age += 1 } // doesn't change the age
func (p *Person) Aging2() { p.Age += 1 } // change the age
func main() {
  me := Person{Age:28}
  me.Aging1() 
  fmt.Println(me.Age) // 28
  me.Aging2() 
  fmt.Println(me.Age) // 29
}

Length 11

That snippet above means that Stringer is a type that could hold anything that already implements String() string function (function named String that returns a string), for example:

A function can have its own owner, this function often called method in another language, to set the ownership of a function, just type the owner between func and the function name, for example

type Person struct {
  Name string
  Age  int
}
func (p Person) Print() {
  fmt.Println("I'm a person named " + p.Name)
}
func main() {
  me := Person{"kis",28}
  me.Print()
}
added 1606 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20

Length 11

interface{}

This is an example of a type that can hold anything. The interface{} translates to "any type that implements at minimum 0 function". Here's better use-case on how to use this keyword

type Stringer interface {
  String() string
}

That snippet above means that Stringer is a type that could hold anything that already implements String() string function (function named String that returns a string), for example:

type Hooman struct {
  Name string
  Age int
}
func (h Hooman) String() string {
  return "I'm a hooman named " + h.Name + " " + strconv.Itoa(h.Age)
}
func main() {
  me := Hooman{}
  me.Name = "kis"
  me.Age = 28
  x := Stringer(me)
  fmt.Println(x.String()) // kis 28
}

Length 10

func X(){}

This is an example of how to create a function. This function named X that holds zero parameter and doesn't return anything. You can add parameters/arguments between the () and return type between the ){, for example:

func Add(a int, b int) int {
  return a + b
}

Or alternative syntax:

func Add(a, b int) (c int) {
  c = a + b
  return
}

A function can have its own owner, this function often called method in another language, to set the ownership of a function, just type the owner between func and function name, for example

type Person struct {
  Name string
  Age  int
}
func (p Person) Print() {
  fmt.Println("I'm a person named " + p.Name)
}

Length 9

Length 9

Length 11

interface{}

This is an example of a type that can hold anything. The interface{} translates to "any type that implements at minimum 0 function". Here's better use-case on how to use this keyword

type Stringer interface {
  String() string
}

That snippet above means that Stringer is a type that could hold anything that already implements String() string function (function named String that returns a string), for example:

type Hooman struct {
  Name string
  Age int
}
func (h Hooman) String() string {
  return "I'm a hooman named " + h.Name + " " + strconv.Itoa(h.Age)
}
func main() {
  me := Hooman{}
  me.Name = "kis"
  me.Age = 28
  x := Stringer(me)
  fmt.Println(x.String()) // kis 28
}

Length 10

func X(){}

This is an example of how to create a function. This function named X that holds zero parameter and doesn't return anything. You can add parameters/arguments between the () and return type between the ){, for example:

func Add(a int, b int) int {
  return a + b
}

Or alternative syntax:

func Add(a, b int) (c int) {
  c = a + b
  return
}

A function can have its own owner, this function often called method in another language, to set the ownership of a function, just type the owner between func and function name, for example

type Person struct {
  Name string
  Age  int
}
func (p Person) Print() {
  fmt.Println("I'm a person named " + p.Name)
}

Length 9

added 556 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 135 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 1347 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 336 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 429 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 154 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading