GoLang Journey – Basic Data Types

You might want to browse the previous post on this series.

Integers

  • Go provides both signed and unsigned integer type
  • There are four different size of signed integer type; 8, 16, 32, 64 bits, represented by int8, int16, int32 and int64
  • Similarly there are four different size of unsigned integer types; 8, 16, 32, 64 bits, represented by uint8, uint16, unit32 and uint64
  • There are also two types called int and unit which is the most efficient size for signed and unsigned integer on a particular platform
  • int is the mostly used type
  • There should not be any assumption about size of int as different compilers make different choice even on identical hardwares
  • rune is a synonym for int32 and is mostly used for a unicode value
  • byte is synonym for uint8
  • uintptr – Unsigned int which is used to hold all bits of a pointer value, length is unspecified
  • Explicit conversion needed for transferring value from one type to another i.e. to say, int32, int64 and int are three different values
  • Signed integer is in 2’s complement form
  • Remainder operator (%) is used only for int
  • The sign of remainder is always the sign of dividend, so (-7%5) and (-7%-5) both values to -2
  • After an arithmetic operation, if the result size is more than what we can represent in the result type, it is said to overflow. The higher order bits are simply discarded

Floating Point Numbers

  • Go provides two size of floating point numbers, float32 and float64
  • float64 should be preferred for most purpose as in case of float32, error accumulates rapidly
  • Digits may be omitted before or after decimal value, .98 and 3. are both legal declaration
  • Scientific notation using e is supported as well and is used in case of very large or very small number
const Avogadro = 6.02214129e23
  • Floating points are printed using %g verb

Complex Numbers

  • Go provides two size of complex numbers, complex64 and complex128 whose components are float32 and float64
  • The built-in functions create a complex number from its real and imaginary components
  • The built-in real and imag functions extract these components
package main
import "fmt"
func main(){
var x complex128 = complex(1, 2)
var y complex128 = complex(3, 4)
fmt.Println(x*y)
fmt.Println(real(x*y))
fmt.Println(imag(x*y))
}
view raw complex.go hosted with ❤ by GitHub
(-5+10i)
-5
10

Boolean

  • Two possible values – True and False
  • Boolean values can be combined with AND and OR operator

Strings

  • A string is an immutable sequence of bytes
  • Text strings are conventionally interpreted as UTF-8 encoded sequences
  • The built-in len function returns the number of bytes in a string
  • Index operation s[i] retrieves the byte at i-th index of string s
  • Attempting to access a byte outside this range results in a panic
  • The substring operation s[i:j] yields a new string consisting of the bytes of the original string starting at index i and continuing up to, but not including, the byte at index j.
  • The i-th byte is not necessarily i-th character as URL encoding of non ASCII requires two or more bytes
  • Strings may be compared with comparison operators like == and < and this comparison is done byte by byte

Constants

  • Constants are the expression whose value is known to the compiler
  • Evaluation of Constant is done at the compile time and not run time
const pi = 3.14159
  • We may omit the right-hand side expression for all but the first of the group, implying that the previous expression and its type should be used again in case of sequence of constants
const (
a=52
b
c= 27
d
)
fmt.Println(a, b, c, d) 

Output :: "52 52 27 27"
  • The constant generator iota may be used to create a sequence of related values without spelling out each one explicitly. This is also known as enums
type Weekday int
const (
Sunday Weekday = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
This declares Sunday to be 0, Monday to be 1, and so on

Read the next post in the series.


Reference

The Go Programming Language – Chapter 3 – Alan Donovan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: