Skip to main content

Golang Data Types

Data types:



 Data types is defined as, it is an attribute that tells to the compiler or interpreter about the types of values.
 In other words we can say that it defines, which type of value can be stored in Go variable.
 
 In Go, data types are divided into four major category.

 1. Basic types (number, string, bool)
 2. Aggregate types (array, structs)
 3. Reference types (pointer, slice, map, channels)
 4. Interface types
 

Go basic types:

 Following are the go basic types.
 1. Number
 2. String
 3. Boolean
 

1. Number:

It tells to the compiler that data is in integer type (like 20, 2.30 etc). Number further divided into different category.
1.1. integer
1.2. float
1.3. complex
 

1.1. Integer data type:

Integer data type has different following types.
1.1.1. int - It stores both 32 and 64 bit signed integer, A 32 bit system allocate 32 bit of memory and a 64 bit system allocate 64 bit of memory. Hence 32bit system can store -2147483648 to 2147483647 and 64bit system can store -9223372036854775808 to 9223372036854775807
1.1.2. uint - It stores same as int, uint stores 32 or 64 bit unsigned integer
1.1.3. int8 - System allocate 8 bit of memory to store signed integer, hence it can store -128 to 127
1.1.4. uint8 - Same as int8, uint8 store unsigned integer, it can store value between 0 to 255
1.1.5. int16 - System allocate 16 bit of memory to store signed integer, hence it can store -32768 to 32767
1.1.6. uint16 - Same as int16, uint16 store unsigned integer, it can store value between 0 to 65535
1.1.7. int32 - System allocate 32 bit of memory to store signed integer, hence it can store -2147483648 to 2147483647
1.1.8. uint32 - Same as int32, uint32 store unsigned integer, it can store value between 0 to 4294967295
1.1.9. int64 - System allocate 64 bit of memory to store signed integer, hence it can store -9223372036854775808 to 9223372036854775807
1.1.10. uint64 - Same as int64, uint64 store unsigned integer, it can store value between 0 to 18446744073709551615

1.1.11. uintptr - It is an integer type that is large enough to hold the bit pattern of any pointer.

1.2. Float data type:

Float data types are those types which ensure the number with decimal (like 1.20, 10.00).
1.2.1. float32 - System allocate 32 bit of memory to store float value, hence it can store values between -3.4E+38 to +3.4E+38
1.2.2. float64 - System allocate 64 bit of memory to store float value, hence it can store values between -1.7E+308 to +1.7E+308
1.2.3. complex64 - complex64 has float32 as real part and float32 as imaginary part
1.2.4. complex128 - complex128 has float64 as real part and float64 as imaginary part

2. String:

It defines for either character or text values. it store string by default in UTF-8, string should be encapsulated in double quote. Its default value is empty string.

3. Boolean:

Boolean data types describe only two values either false or true, its default value is false.

Note: Further types will discuss in another chapter.

Comments

Popular posts from this blog

Apache Tika server using Golang

Apache Tika?      Apache Tika is a toolkit/library that uses to detect meta data and extract contents from different types of files (such as .txt, .docx, .pdf, .ppt etc).      Tika parsed all types of file through single interface, with Tika make useful for search engine indexing, content analysis and much more. Configuration To setup a tika server on local machine, you need to go download   page. Which looks like below image. From download page just download with the 3rd link Viz. "Mirrors for tika-server-1.25.jar". After download completion, go to the downloaded file and open a command prompt. Inside command prompt type below command to start tika server. java -jar tika-server-1.25.jar Now your tika server has been started on local environment and it'll provide an URL to access tika server. http://localhost:9998/ After configure tika server go to your GOPATH directory, create a file with name as you want to give file name with .go extension. I'm taking as "

What is Go/golang?

“Go will be the server language of the future.” — Tobias Lütke, Shopify Go (often referred to as Golang) is an open source programming language that makes it easy to build simple, reliable, and efficient software. Golang is a statically type language and compiled programing language. Go is similar to C, but with memory safety, garbage collection. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code. The world was first introduced to Go in November 2009 by Google’s Rob Pike, Robert Griesemer, and Ken Thompson. The main goal of creating Go was to combine the best features of other programming languages. Go released its latest version 1.15.6 on 3rd December 2020. A very basic Hello world program with Go: package main import "fmt" func main () { fmt . Println ( "Hell

Go Conditional Statements and Loops

Conditional statement is those statement, which is used to execute code on a certain condition. Unlike C or Java conditional statement, conditions are not required to be written inside parenthesis in Go. Lets dive into different conditional statement. Go has following conditional statements are: 1. If condition 2. If-else condition 3. If-else if-else condition 1. If condition: If condition used to check true statement only, means if condition is true then the enclosed code will be executed other wise do nothing. Suppose we've a variable with value as "Mango" and we want some task to perform while condition is true, so above line of code will execute with true condition and will print the message. If the condition isn't true then the code will do nothing. 2. if-else condition:          Another form of conditional statement is " If-else" condition, it has two part, the first part will execute while the condition will be true otherwise the second pa