Software Development

What are Golang Types

Go is a strongly typed language and type is life. This language has rich types and good support for extension of type. Type provides integrity.

In this post I will share some of the primitive types and how Go handles them.

Everything is 0 or 1 in computer and only these 2 values are used to represent any values we want.

Arrangement of 0 or 1 tells what is the value.

Take a example of byte value at some memory location:

Golang Types

What is it ? You need type information .

If type is int then value is 10, if type is enum then we have some other value.

Type information tell us about value and size for eg if type is Boolean then it tells it is single byte value.

Information about types supported by Go can be found at  Lang Spec Types  page.

How to declare variable ?

var variablename type

variablename := value // Short declaration

Both of the above declare a variable but the way it is initialized is very different.

Var creates and initialized with ZERO value of its type,  Zero value is very special it makes code bug free and clean! No null checks.

Zero value is based on Type so for integer type it is zero, boolean it is false , string it is empty.

Go has some type like int that gets size based on underlying architecture, for eg it will be 4 bytes (i.e 32 bit arch) or 8 bytes ( 64 bit arc). This is also a good example of mechanical sympathy to underlying platform.

Examples of variable declaration:

var value int
	var f float64
	var b bool
	var by byte
	var name string
	var x rune

	//Variable are declared and initialized by compiler to ZERO VALUE of its type
	//https://golang.org/ref/spec#The_zero_value

	fmt.Printf("value %T -> (%v) \n", value, value)
	fmt.Printf("f %T -> (%v) \n", f, f)
	fmt.Printf("b %T -> (%v) \n", b, b)
	fmt.Printf("by %T -> (%v) \n", by, by)
	fmt.Printf("name %T -> (%v) \n", name, name)
	fmt.Printf("x %T -> (%v) \n", x, x)

	fmt.Println("*******************")

	value1 := 10
	f1 := 3.14
	b1 := true
	name1 := "Say Hello"
	x1 := 100
	fmt.Printf("value %T -> (%v) \n", value1, value1)
	fmt.Printf("f %T -> (%v) \n", f1, f1)
	fmt.Printf("b %T -> (%v) \n", b1, b1)
	fmt.Printf("name %T -> (%v) \n", name1, name1)
	fmt.Printf("x %T -> (%v) \n", x1, x1)

Alias for built in type

This is a very powerful feature and it allow to built in types to be extended by adding behavior .

Example of type alias:

//Create Alias for int type
type RichInt int

func main() {

	var ri RichInt
	ri = 100

	fmt.Println("Value of rich int", ri)
	fmt.Println("Convert to Int", int(ri))
	fmt.Println("From int to Rich Int", RichInt(100))
	fmt.Println("Binary Value", ri.toBinary())
}

In the above example RichInt has toBinary function that returns binary value.  I will share later how to extend types when we explore methods of types.

Casting Vs Conversion

Casting is magic, it allows to convert one type to another implicitly. How many times in java you lost a value when long/int casting or double/float?

Go has a concept of conversion. You explicitly convert from x to y type and pay the cost of extra memory at the cost of safety.

Go lang spec has some good examples.

Some real custom types

Go lang has support for Struct type, it is pure value type , no noise of behavior attached to it.

It gives control of memory layout, you can choose really compact memory layout to avoid padding or to add padding if required.

Struct can be declared like below:

type person struct {
 firstName string
 lastName  string
        age int
}

Once struct is defined then we can create value of struct type.

It is value not object just remember that!

Value can be created using the below code:

var p1 person

The above code created a value and initialized it with zero value, string is initialized to empty value and int to 0.

No null check is required when processing p1 because it is initialized to ZERO value.

Short declaration can be used to specified non zero or other value:

p2 := person{firstName: "James", lastName: "Bond", age: 35}

Zero value and convenient way to creating value kills the need of having constructor or destructor in Go.

You can now start seeing power of value. No overhead of constructor/destructor/ or complex life cycle.

I know you will have question on what about special init code or clean up code that is required ?

Behavior are handled very differently, we will go over that in a later post.

Struct can be nested also and Zero value or short declaration works like magic!

We will create additional struct:

ype address struct {
 address1 string
 address2 string
 city     string
}

type contact struct {
 landLine int
 mobile   int
}

type person struct {
 firstName      string
 lastName       string
 age            int
 add            address
 contactDetails contact
}

p3 := person{firstName: "James", lastName: "Bond", age: 35,
  add:            address{address1: "30 Wellington Square", address2: "Street 81"},
  contactDetails: contact{mobile: 11119999}}

Published on Java Code Geeks with permission by Ashkrit Sharma, partner at our JCG program. See the original article here: what are Golang Types

Opinions expressed by Java Code Geeks contributors are their own.

Ashkrit Sharma

Pragmatic software developer who loves practice that makes software development fun and likes to develop high performance & low latency system.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button