Translate

Friday, 22 August 2014

Types in Go Lang

Understanding the way “Types in Go Lang” work is a very essential step in order to write better and precise programming.

Go is a statistically typed language. That means, each variable has a type and once type is given to a variable that type cannot be changed. But of course there are situations where we need to switch between types using dynamic typing.

· There are two Types in Go Lang

1.Named types : A type that is declared with pre declared type.Pre declared types: Boolean, Numeric and String types

2.Unnamed types: A type that is declared using previously declared types or a composition. Composite types:array, struct, pointer, function, interface, slice, map, and channel types. Composite types are constructed using literals

·Static or Dynamic type

1.Static type: A type of a variable when it is declared. Generally all types are static types. So once a variable is declared with a type it does not change

2. Dynamic type: Dynamic type is only applicable for interface variables. A type varies during the execution of the program. Ultimately any dynamic type should be assigned to a static type. I have explained more about dynamic type in type assertion section below.

 Rules to follow in types


· Implicit conversion is not possible from one type to another type in Go Lang, even though the value belongs to a pre defined type. So unlike in other programming languages int16 cannot be implicitly converted to int32.

var age uint8 = 47
var newage int16 = int16(age)// This is called explicit //conversion. Since age is uint8 explicitly converted



·  Unlike Java and C#, Go does not differentiate stack and heap memory.

· Methods can be assigned only to the types declared in the same package. Take a look at the example below. 


func (s string) Length(str string) int {
    return len(str)
}
func main() {
    var name string = "About types  in Go Lang"
    fmt.Printf(name.Length(name))
}

The above program compiles an error, because string type is not declared in the same package. String is a pre declared type; hence, the rule is receiver of the method should be defined in the same package.

·  The below figure explains, how to create type from the underlining primitive types and how to create type from an existing type. It also explains type casting between types



                          Creating types from underlying types
Download above program from Creating and casting types
Type assertions
As all of us know and have experienced the importance of dynamic types in Java and C# programming. Dynamic types are very useful when using generic frameworks and design patterns. Dynamic type in Go Lang can be created by using empty interface {}.See the below code snippet


//The below types are declared dynamically using empty interface
 var name interface{} ="Jiten"
 var age interface{} = 33
  var address interface{} ="Bur Dubai, Dubai"

Once a value is assigned to a dynamic type, at any point of time it has to be assigned to its original type. Similar concept is boxing and unboxing in .NET family languages. Go’s way of dynamic type to static type is called type assertion. The below figure explains how to cast from dynamic type to variable’s actual type.

Type assertion & casting
  Download above program from Type assertion and casting
 
So far in this post, I gave overall information about types. Going forward I will post more specific about Pointer, Array, Slice, Map, Func, Struct , Interface and Channel types. Each post would give precise information about each of the types.

Enjoy Go Lang, stay tuned, thanks for reading.

No comments:

Post a Comment