Go Kind vs Type: Mastering Golang's Type System šŸŽÆ

beginner
19 min

Go Kind vs Type: Mastering Golang's Type System šŸŽÆ

Introduction šŸ“

Welcome to our deep dive into the world of Go programming language! In this comprehensive lesson, we'll explore Go's unique approach to handling types and interfaces, specifically focusing on Kind and Type. By the end of this tutorial, you'll have a solid understanding of these concepts and be able to apply them in your projects.

Go Types šŸ’”

In Go, a type defines the set of values that can be stored in a variable and the operations that can be performed on those values. There are several types in Go:

  1. Built-in types: These are the basic data types provided by the language such as int, float, string, and bool.

  2. User-defined types: These are custom types created by the programmer, including structures (struct), pointers (*), slices ([]), maps (map), channels (chan), and functions (func).

Go Kinds šŸ“

While Go types define the data structure and operations, the Kind describes the nature of the value. Go's compiler assigns a Kind to every value at runtime, and it can be one of the following:

  1. Invalid: A value with no associated type.
  2. Bool: A boolean value (true or false).
  3. Int: An integer value (int8, int16, int32, int64, uint8, uint16, uint32, uint64).
  4. Float: A floating-point value (float32, float64).
  5. String: A string value.
  6. Struct: A user-defined structure.
  7. Slice: A slice (array with a dynamic size).
  8. Array: A fixed-size, zero-indexed sequence of elements.
  9. Chan: A channel (used for communication between goroutines).
  10. Interface: An interface type (more on this later).
  11. Func: A function value.
  12. Ptr: A pointer (address of another value).
  13. Map: A key-value pair collection.
  14. nil: A special value that represents the absence of a value.

Types vs Kinds šŸ’”

Although Type and Kind may seem similar, it's important to understand the difference:

  • A Type defines the data structure and operations that can be performed on a value.
  • The Kind describes the nature of the value at runtime.

For example, consider the following code snippet:

go
package main import "fmt" func main() { var x int = 10 fmt.Println("Type of x:", typeof(x)) fmt.Println("Kind of x:", kindOf(x)) } func typeof(i interface{}) string { switch i.(type) { case int: return "int" case float64: return "float64" case string: return "string" default: return "other" } } func kindOf(i interface{}) string { return reflect.TypeOf(i).Name() }

In this example, we create two functions: typeof and kindOf. The typeof function checks the type of a given value, while the kindOf function checks the Kind of the value. Running this code will output:

Type of x: int Kind of x: int

šŸ“ Note: In Go, the reflect package provides functions to inspect the runtime type of values, including their Kind.

Practice Time šŸŽÆ

Quick Quiz
Question 1 of 1

What is the `Kind` of the following value in the Go code below?

Stay tuned for the next part of this lesson, where we'll dive deeper into Go's interfaces and how they relate to types and kinds! šŸš€