Algorithm
In the Go programming language (often referred to as Golang), data types are classified into several categories, including basic types, composite types, and reference types. Here's an overview of the main data types in Go:
Basic Types:
-
Numeric Types:
int
: Integer type (platform-dependent size).int8
,int16
,int32
,int64
: Signed integers of various sizes.uint
: Unsigned integer type (platform-dependent size).uint8
,uint16
,uint32
,uint64
: Unsigned integers of various sizes.float32
,float64
: Floating-point types.
-
String Type:
string
: Represents a sequence of characters.
-
Boolean Type:
bool
: Represents boolean values,true
orfalse
.
Composite Types:
-
Array:
array
: Fixed-size sequence of elements of the same type.
-
Slice:
slice
: Dynamic-sized, flexible view into elements of an array.
-
Map:
map
: Unordered collection of key-value pairs.
-
Struct:
struct
: Aggregate data type that groups together variables (fields) under a single name.
Reference Types:
-
Pointer:
pointer
: Holds the memory address of a variable.
-
Function:
func
: Represents a function type.
-
Interface:
interface
: Defines a set of method signatures that a type must implement.
-
Channel:
chan
: Used for communication between goroutines.
Other Types:
-
Complex Types:
complex64
,complex128
: Complex numbers with float32 and float64 parts.
-
Type Aliases:
type
: Allows the declaration of an alternative name for an existing type.
Go is statically typed, which means that the type of a variable is known at compile time. It also has a strong type system, so explicit type conversions are required in certain situations.
Code Examples
#1 Understanding Integer Type
Code -
Golang Programming
package main
import "fmt"
func main() {
var integer1 int
var integer2 int
integer1 = 5
integer2 = 10
fmt.Println(integer1)
fmt.Print(integer1)
}
Copy The Code &
Try With Live Editor
Output
10
#2 Understanding Float Type
Code -
Golang Programming
// Program to illustrate float32 and float64 with example
package main
import "fmt"
func main() {
var salary1 float32
var salary2 float64
salary1 = 50000.503882901
// can store decimals with greater precision
salary2 = 50000.503882901
fmt.Println(salary1)
fmt.Println(salary2)
}
Copy The Code &
Try With Live Editor
Output
50000.503882901
#3 Understanding String Type
Code -
Golang Programming
// Program to create string variables
package main
import "fmt"
func main() {
var message string
message = "Welcome to DevsEnv"
fmt.Println(message)
}
Copy The Code &
Try With Live Editor
Output
#4 Understanding bool Type
Code -
Golang Programming
// Program to create boolean variables
package main
import "fmt"
func main() {
var boolValue bool
boolValue = false
fmt.Println(boolValue)
}
Copy The Code &
Try With Live Editor
Output
Demonstration
Go Data Types-Go Long Programming