Datatypes in Golang

In Go, a data type is a category of types that represent a value with a specific set of characteristics. There are several built-in data types in Go, including:

  1. bool: a boolean type that can be either true or false.
  2. string: a sequence of Unicode characters.
  3. int, int8, int16, int32, int64: signed integer types with different sizes (8, 16, 32, and 64 bits, respectively).
  4. uint, uint8, uint16, uint32, uint64: unsigned integer types with different sizes (8, 16, 32, and 64 bits, respectively).
  5. float32, float64: floating-point types with 32-bit and 64-bit sizes, respectively.
  6. complex64, complex128: complex number types with float32 and float64 components, respectively.

Go also supports composite data types, which are types that are made up of other types. The composite data types in Go include:

  1. array: a fixed-length sequence of elements of the same type.
  2. slice: a variable-length sequence of elements of the same type.
  3. struct: a collection of fields, each of which has a name and a type.
  4. pointer: a type that refers to the memory address of another value.
  5. function: a type that represents a function.
  6. interface: a type that specifies a set of methods that a value must implement.

Go also supports type aliases, which allow you to define a new name for an existing type. This can be useful for creating shorter or more descriptive names for types, or for creating distinct types that are based on existing types but have different semantics.

Here are some examples of using the built-in data types in Go:

// Declare a struct type called "Person" with two fields: "Name" (a string) and "Age" (an int)
type Person struct {
    Name string
    Age  int
}

// Create a new Person value and assign values to its fields
var p Person
p.Name = "Alice"
p.Age = 30

// Create a new Person value using a struct literal
p := Person{"Bob", 40}

Here is an example of using a composite data type in Go:

// Declare a struct type called "Person" with two fields: "Name" (a string) and "Age" (an int)
type Person struct {
    Name string
    Age  int
}

// Create a new Person value and assign values to its fields
var p Person
p.Name = "Alice"
p.Age = 30

// Create a new Person value using a struct literal
p := Person{"Bob", 40}

Here is an example of using a type alias in Go:

// Define a new type called "ByteSize" that is an alias for int64
type ByteSize int64

// Declare a variable of type ByteSize and initialize it to 1KB
var size ByteSize = 1 << 10

// Define a function that takes a ByteSize value as an argument
func (b ByteSize) String() string {
    // ...
}