Sunday, 5 May 2024

GO Language : Basic Data Types

 

  1. Numeric Types:

    • int: Signed integers, size depends on the architecture (32 or 64 bits).
    • int8, int16, int32, int64: Signed integers with specific sizes (8, 16, 32, or 64 bits).
    • uint: Unsigned integers, size depends on the architecture.
    • uint8, uint16, uint32, uint64: Unsigned integers with specific sizes.
    • float32, float64: Floating-point numbers.
    • complex64, complex128: Complex numbers with float32 and float64 real and imaginary parts.
  2. Boolean Type:

    • bool: Represents boolean values, either true or false.
  3. String Type:

    • string: Represents a sequence of characters.

Composite Data Types:

  1. Arrays:

    • An array is a fixed-size collection of elements of the same type.
    • Syntax: [size]type, e.g., var arr [5]int.
  2. Slices:

    • A slice is a dynamic and flexible representation of an array segment.
    • Syntax: []type, e.g., var slice []int.
  3. Maps:

    • A map is an unordered collection of key-value pairs.
    • Syntax: map[keyType]valueType, e.g., var m map[string]int.
  4. Structs:

    • A struct is a composite data type that groups together variables under a single name.
    • Syntax: type StructName struct { /* fields */ }, e.g.,
      go
      type Person struct { Name string Age int }

Other Data Types:

  1. Pointers:

    • A pointer is a variable that stores the memory address of another variable.
    • Syntax: *type, e.g., var ptr *int.
  2. Functions:

    • Functions are first-class citizens in Go and can be assigned to variables, passed as arguments, and returned from other functions.
    • Syntax: func(parameters) returnType { /* body */ }, e.g.,
      go
      func add(a, b int) int { return a + b }
  3. Interfaces:

    • An interface is a type that specifies a set of method signatures.
    • Syntax: type InterfaceName interface { /* method signatures */ }, e.g.,
      go
      type Shape interface { Area() float64 Perimeter() float64 }

These are the primary data types in Go that you'll encounter while programming. Understanding these data types and their respective use cases is essential for effective Go programming. Experiment with each data type and explore their functionalities to become proficient in Go development.

No comments:

Post a Comment

Interactive Report: Introduction to the Internet of Things (IoT) ...

Popular Posts