> For the complete documentation index, see [llms.txt](https://ineelhere.gitbook.io/code-with-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ineelhere.gitbook.io/code-with-go/23-generics.md).

# 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:

```go
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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/23-generics.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
