arrays, structures AND POINTERS in golang

a

We previously used something called Integers which look like this

var a int = 5 which was super useful. But suppose let’s say you want to store a collection of integer not a single integer.

like my mark in this semester -2, -10, 8. Yes by the way you can also store negative numbers in GO. So whatever, how can we store this collection of elements? Here comes array.

SYNTAX:

var variable_name = [SIZE] type {values}

Here an example helps:

package main

import "fmt"

func main() {
	var a = [5]int{1, 2, 5, 8, 3}

	fmt.Println(a)
}

But what if, you want to get the value at the 3rd or 4th position. Well, one thing to do here is to something like this:

package main

import "fmt"

func main() {
  var a = [5] int {1, 2, 5, 8, 3}
  
	fmt.Println("value at 3rd pos:", a[3])
}

Mmmmm. sure it worked. But what we want is 5 not 8. right?

In many programming languages the position(index in programming terms) starts from 0 not 1.

So in this case, 0 -> 1, 1->2, 2->5, 3->8, 4->3

So If you want to get the value at 3rd position you should say 2 not 3.

Array indexing always starts at 0

(well there are exceptions though. In language R index starts at 1)
package main

import "fmt"

func main() {
   var a int = 10   
   fmt.Printf("Address of a: %x", &a  )
}

POINTERS

Every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory.

pointer is a variable whose value is the address of another variable. The asterisk * you used to declare a pointer.

syntax:

var var_name *type

package main
import "fmt"
func main() {
	var a int = 20 /* variable declaration */
	var ip *int    /* pointer variable declaration */

	ip = &a /* store address of 'a' in pointer */
	fmt.Printf("Address of a variable a: %x\n", &a)

	/* address of pointer variable */
	fmt.Printf("Address stored in ip variable: %x\n", ip)

	/*value using the pointer */
	fmt.Printf("Value of *ip variable: %d\n", *ip)

	//changing a affects ip
	a = 10
	fmt.Printf("Value of *ip variable: %d\n", *ip)

	//changing the value of a using *ip
	*ip = 50
	fmt.Printf("value of a variable: %d\n", a)
}

You can read more about this here. https://en.wikipedia.org/wiki/Pointer_(computer_programming)

structures

Structure is another user-defined data type which allows you to combine data items of different kinds unlike arrays.

syntax:

type StructName struct{
    field1 fieldType1
    field2 fieldType2
}

Here’s a simple program:

package main

import "fmt"

type person struct {
	name string
	age  int
}

func main() {
	var obj person
	obj.name = "vwar"
	obj.age = 19

	fmt.Print(obj)
}

You can also declare objects like this:

package main

import "fmt"

type person struct {
	name string
	age  int
}

func main() {

	//syntax to create a person obj
	fmt.Println(person{"vignesh", 19})

	s := person{name: "Tej", age: 17}
	fmt.Println(s.name)

	sp := &s
	fmt.Println(sp.age)

	sp.age = 51
	fmt.Println(sp.age)
	fmt.Println(s.age)
}

Phew. Thats almost what I want to cover.

Keep practicing

bye.

About the author

vigneshwar

Add comment

Leave a Reply

By vigneshwar

Most common tags

%d bloggers like this: