Development with Go
- Manjitsing K. Valvi
Type Conversion
● Casting ⇒ change of the type without transformation of its content
● Go only works with conversion
● Expression T(v) converts the value v to the type
● Assignment between items of different type requires an explicit
conversion
Type Conversion
● Example
var a int16 = 511 // Signed Integer 16 bit +ve
fmt.Printf("cast int8: %dn", int8(a)) // Integer with a smaller size
fmt.Printf("cast uint8: %dn", uint8(a)) // Unsigned Integer
fmt.Printf("cast int32: %dn", int32(a)) // Integer with bigger size
O/P:
cast int8: -1 // 11111111
cast uint8: 255
cast int32: 511
var a int8 = -127 // Signed Integer 8 bit -ve
fmt.Printf("cast uint8: %dn", uint8(a)) // Unsigned Integer
fmt.Printf("cast int16: %dn", int16(a)) // Integer with bigger size
O/P:
cast uint8: 129 // 10000001
cast int16: -127
GO Composite types
Arrays
● Fixed-length sequences of the same type
● Arrays in go are values
○ When you assign an array to another variable, it copies the entire array
○ When you pass an array as an argument to a function, it makes an entire
copy of the array instead of passing just the address
Go Array
● Declaration
arrayName := [n]Type{val1, val2, val3} // n is size of array
arrayName2:= [n]Type{} // no values specified
● We cannot assign an array to a different array of the same type but
different length
● When you pass an array as a function argument, then size is also part of it.
Go Structs
● Named collection of fields, can be of different types
● Acts as a container of related data of heterogeneous data type
● Syntax :
type structName struct {
var1 type1
var2 type2
var3 type3
}
Go Structs
type student struct {
name string
roll int
gpa float64
}
//Initialize a struct without named fields
student1 := student{"ABC", 191401, 8.4}
fmt.Println(student1)
//Initialize a struct with named fields
student2 := student{
name: "PQR",
roll: 191402,
gpa: 7.5,
}
Go Slices
● A flexible and extensible data structure for collections of data
● an abstraction over Go array
● Consists of:
● a pointer to the array
● the length of the segment
● slice capacity
● Creating
1. sliceName := []Type{val1, val2, val3} //literal
2. sliceName := make([]Type, length, [capacity])//make func
3. arrayName := [n]Type{val1, val2, val3} //slicing
sliceName := arrayName[startIndex:lastIndex]
References
● “The Way to Go: A Thorough Introduction to the Go Programming Language”,Ivo Balbaert
● “The Go Programming Language”,Alan Donovan,Brian Kernighan,Addison-Wesley
● https://gobyexample.com/slices
● https://medium.com/@marty.stepien/arrays-vs-slices-bonanza-in-golang-fa8d32cd2b7c
● https://blog.golang.org/slices-intro
● https://www.godesignpatterns.com/2014/05/arrays-vs-slices.html

Composite types

  • 1.
    Development with Go -Manjitsing K. Valvi
  • 2.
    Type Conversion ● Casting⇒ change of the type without transformation of its content ● Go only works with conversion ● Expression T(v) converts the value v to the type ● Assignment between items of different type requires an explicit conversion
  • 3.
    Type Conversion ● Example vara int16 = 511 // Signed Integer 16 bit +ve fmt.Printf("cast int8: %dn", int8(a)) // Integer with a smaller size fmt.Printf("cast uint8: %dn", uint8(a)) // Unsigned Integer fmt.Printf("cast int32: %dn", int32(a)) // Integer with bigger size O/P: cast int8: -1 // 11111111 cast uint8: 255 cast int32: 511 var a int8 = -127 // Signed Integer 8 bit -ve fmt.Printf("cast uint8: %dn", uint8(a)) // Unsigned Integer fmt.Printf("cast int16: %dn", int16(a)) // Integer with bigger size O/P: cast uint8: 129 // 10000001 cast int16: -127
  • 4.
    GO Composite types Arrays ●Fixed-length sequences of the same type ● Arrays in go are values ○ When you assign an array to another variable, it copies the entire array ○ When you pass an array as an argument to a function, it makes an entire copy of the array instead of passing just the address
  • 5.
    Go Array ● Declaration arrayName:= [n]Type{val1, val2, val3} // n is size of array arrayName2:= [n]Type{} // no values specified ● We cannot assign an array to a different array of the same type but different length ● When you pass an array as a function argument, then size is also part of it.
  • 6.
    Go Structs ● Namedcollection of fields, can be of different types ● Acts as a container of related data of heterogeneous data type ● Syntax : type structName struct { var1 type1 var2 type2 var3 type3 }
  • 7.
    Go Structs type studentstruct { name string roll int gpa float64 } //Initialize a struct without named fields student1 := student{"ABC", 191401, 8.4} fmt.Println(student1) //Initialize a struct with named fields student2 := student{ name: "PQR", roll: 191402, gpa: 7.5, }
  • 8.
    Go Slices ● Aflexible and extensible data structure for collections of data ● an abstraction over Go array ● Consists of: ● a pointer to the array ● the length of the segment ● slice capacity ● Creating 1. sliceName := []Type{val1, val2, val3} //literal 2. sliceName := make([]Type, length, [capacity])//make func 3. arrayName := [n]Type{val1, val2, val3} //slicing sliceName := arrayName[startIndex:lastIndex]
  • 9.
    References ● “The Wayto Go: A Thorough Introduction to the Go Programming Language”,Ivo Balbaert ● “The Go Programming Language”,Alan Donovan,Brian Kernighan,Addison-Wesley ● https://gobyexample.com/slices ● https://medium.com/@marty.stepien/arrays-vs-slices-bonanza-in-golang-fa8d32cd2b7c ● https://blog.golang.org/slices-intro ● https://www.godesignpatterns.com/2014/05/arrays-vs-slices.html