Data-Types

The capabilities of data-types in different languages.

Go Data-Types


Primitive Types

Basic data types provided by the language (e.g., integers, floats, strings).

Structured Data Type Definitions (Structs)

Golang structures data using structs, with optional fields using pointers.

type User struct { ID int Name string Email *string // Optional field using a pointer } func main() { email := "alice@example.com" user := User{ID: 1, Name: "Alice", Email: &email} fmt.Println(user) }
  • struct defines structured data.
  • Optional fields use pointers (*string).

Error Loading

Missing


Structured Data Type Definitions

Definitions for structured data types like Dataclasses, Dictionaries, or TypedDict.

Classes

type Person struct { Name string } // Constructor function (Go does not have a built-in constructor) func NewPerson(name string) *Person { fmt.Println(name, "is created") return &Person{Name: name} } // Method attached to struct func (p *Person) Greet() { fmt.Println("Hello, my name is", p.Name) } // Destructor simulation using `defer` func DeletePerson(p *Person) { fmt.Println(p.Name, "is deleted") } func main() { person := NewPerson("Alice") person.Greet() // Simulating destructor with `defer` defer DeletePerson(person) }
  • No real classes, just the ability to associate a function with a structured type
  • Destructor function via defer keyword - this defers execution until the function has completed (similar to finally in other languages but at function scope)

Missing


Classes

Blueprints for creating objects with attributes and methods.

Class Property Accessors

Interfaces don't have property getters and setters, but the interface and struct can be separated with property wrappers as functions:

type NamedEntity interface { GetName() string SetName(newName string) } type Person struct { name string } // Implementing the interface func (p *Person) GetName() string { return p.name } func (p *Person) SetName(newName string) { p.name = newName } func main() { var entity NamedEntity = &Person{name: "Charlie"} fmt.Println("Before:", entity.GetName()) entity.SetName("David") fmt.Println("After:", entity.GetName()) }

Missing


Class Property Accessors

Methods to get or set class properties.

Class or Struct Generics

type Box[T any] struct { Content T } func main() { intBox := Box[int]{Content: 10} fmt.Println(intBox.Content) // 10 strBox := Box[string]{Content: "Golang"} fmt.Println(strBox.Content) // Golang }

Missing


Class Generics

Support for generic programming with classes.

Class Inheritance

Using struct embedding to emulate inheritance:

type Animal struct { Name string } func (a Animal) MakeSound() { fmt.Println("Some generic animal sound") } // Dog "inherits" from Animal via embedding type Dog struct { Animal } func (d Dog) MakeSound() { fmt.Println("Woof!") } func main() { dog := Dog{Animal{Name: "Buddy"}} dog.MakeSound() // "Woof!" }

Interface is supported for structs:

type Speakable interface { Speak() } type Robot struct{} func (r Robot) Speak() { fmt.Println("Beep boop!") } func main() { var bot Speakable = Robot{} bot.Speak() // "Beep boop!" }

Base class constructors implemented via special functions that create object:

type Animal struct { Name string } func NewAnimal(name string) Animal { return Animal{Name: name} } type Dog struct { Animal } func NewDog(name string) Dog { return Dog{Animal: NewAnimal(name)} } func main() { dog := NewDog("Buddy") fmt.Println(dog.Name) // "Buddy" }

Missing


Class Inheritance

Mechanism to create a new class from an existing class.

Enum Definitions (Using `iota`)

Golang does not have native enums but uses const with iota for enumerations.

type Status int const ( Pending Status = iota InProgress Completed ) func main() { currentStatus := InProgress fmt.Println(currentStatus) // Output: 1 }
  • iota generates sequential integer values automatically.
  • Explicit string-based enums require custom methods.
func (s Status) String() string { return [...]string{"Pending", "In Progress", "Completed"}[s] }

Missing


Enum Definitions

Define a set of named constants.

Type Casting

Go provides mechanisms for type conversion and type assertion. These allow you to convert between compatible types or check and extract the underlying type of an interface.

Type Conversion (Base Type Casting) - in Go, type casting is done explicitly using type conversion syntax. This is used to convert between compatible types, such as int to float64.

func main() { var a int = 42 var b float64 = float64(a) // Convert int to float64 var c int = int(b) // Convert float64 back to int fmt.Println(a, b, c) // Output: 42 42.0 42 }
  • Type conversion is explicit and must be specified by the developer.
  • Incompatible types (e.g., string to int) cannot be converted directly.

Type assertion is used to extract the concrete value of an interface. It checks whether the interface holds a specific type.

func main() { var i interface{} = "hello" // Assert that the interface holds a string s, ok := i.(string) if ok { fmt.Println("String value:", s) // Output: String value: hello } else { fmt.Println("Not a string") } }
  • i.(string) asserts that i holds a string.
  • The second return value (ok) is a boolean indicating whether the assertion succeeded.

The type switch statement is used to handle multiple types in a single block. It is particularly useful when working with interfaces.

func describe(i interface{}) { switch v := i.(type) { case int: fmt.Println("Integer:", v) case string: fmt.Println("String:", v) case bool: fmt.Println("Boolean:", v) default: fmt.Println("Unknown type") } } func main() { describe(42) // Output: Integer: 42 describe("hello") // Output: String: hello describe(true) // Output: Boolean: true describe(3.14) // Output: Unknown type }
  • The type switch uses .(type) to determine the type of the value held by an interface.
  • Each case handles a specific type.

Go allows you to perform type assertion and assignment in a single if statement. This is useful for concise type checking.

func main() { var i interface{} = 42 if v, ok := i.(int); ok { fmt.Println("Integer value:", v) // Output: Integer value: 42 } else { fmt.Println("Not an integer") } }
  • The if statement assigns the value to v and checks if the assertion succeeded (ok).
  • This pattern is commonly used when working with interfaces.

Missing


Type Casting

Convert a value from one type to another.

Missing

Missing