Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Generics

This Go program defines a generic function MapKeys and a generic linked list List.

The MapKeys function takes a map and returns a slice containing all the keys of the map.

The List type is a generic linked list with methods to push elements and retrieve all elements.

Let's go through each part of the code with inline comments and additional explanations:

package main

import "fmt"

// MapKeys is a generic function that takes a map and returns a slice containing all keys of the map.
func MapKeys[K comparable, V any](m map[K]V) []K {
    r := make([]K, 0, len(m))
    for k := range m {
        r = append(r, k)
    }
    return r
}

// List is a generic linked list with elements of type T.
type List[T any] struct {
    head, tail *element[T]
}

// element is a generic element in the linked list.
type element[T any] struct {
    next *element[T]
    val  T
}

// Push adds a new element to the end of the list.
func (lst *List[T]) Push(v T) {
    if lst.tail == nil {
        lst.head = &element[T]{val: v}
        lst.tail = lst.head
    } else {
        lst.tail.next = &element[T]{val: v}
        lst.tail = lst.tail.next
    }
}

// GetAll returns all elements in the list as a slice.
func (lst *List[T]) GetAll() []T {
    var elems []T
    for e := lst.head; e != nil; e = e.next {
        elems = append(elems, e.val)
    }
    return elems
}

func main() {
    // Create a map with integer keys and string values
    var m = map[int]string{1: "2", 2: "4", 4: "8"}

    // Call the MapKeys function to retrieve all keys of the map and print them
    fmt.Println("keys:", MapKeys(m))

    // Another way to call MapKeys with explicit types
    _ = MapKeys[int, string](m)

    // Create a generic linked list of integers
    lst := List[int]{}
    lst.Push(10)
    lst.Push(13)
    lst.Push(23)

    // Retrieve all elements from the list and print them
    fmt.Println("list:", lst.GetAll())
}

Output

keys: [1 2 4]
list: [10 13 23]

Explanation:

  1. The MapKeys function is a generic function that accepts a map with keys of type K and values of type V. It iterates over the keys of the map and appends them to a slice, which is then returned.

  2. The List type is a generic linked list that contains elements of type T. It has a head and tail pointers to keep track of the list.

  3. The element type is a generic struct representing an element in the linked list, with a next pointer to the next element and a val field representing the value of the element.

  4. The Push method of the List type adds a new element to the end of the linked list.

  5. The GetAll method of the List type retrieves all elements from the linked list and returns them as a slice.

  6. In the main function, a map with integer keys and string values is created, and the MapKeys function is used to retrieve and print all keys of the map.

  7. Another way to call MapKeys is shown with explicit types.

  8. A generic linked list of integers is created, elements are pushed onto the list, and all elements are retrieved and printed using the GetAll method.

PreviousStruct EmbeddingNextError

Last updated 1 year ago

Was this helpful?