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:

  1. 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.
  2. String Type:

    • string: Represents a sequence of characters.
  3. Boolean Type:

    • bool: Represents boolean values, true or false.

Composite Types:

  1. Array:

    • array: Fixed-size sequence of elements of the same type.
  2. Slice:

    • slice: Dynamic-sized, flexible view into elements of an array.
  3. Map:

    • map: Unordered collection of key-value pairs.
  4. Struct:

    • struct: Aggregate data type that groups together variables (fields) under a single name.

Reference Types:

  1. Pointer:

    • pointer: Holds the memory address of a variable.
  2. Function:

    • func: Represents a function type.
  3. Interface:

    • interface: Defines a set of method signatures that a type must implement.
  4. Channel:

    • chan: Used for communication between goroutines.

Other Types:

  1. Complex Types:

    • complex64, complex128: Complex numbers with float32 and float64 parts.
  2. 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

x
+
cmd
5
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

x
+
cmd
50000.504
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

x
+
cmd
Welcome to DevsEnv

#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

x
+
cmd
false
Advertisements

Demonstration


Go Data Types-Go Long Programming