Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Generics #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 80 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
19. [Snippets](#snippets)
* [Files Embedding](#files-embedding)
* [HTTP Server](#http-server)
20. [Generics](#generics)
* [Type Parameters](#type-parameters)
* [Constraints](#constraints)
* [Generic Types](#generic-types)

## Credits

Expand Down Expand Up @@ -247,7 +251,7 @@ float32 float64
complex64 complex128
```

All Go's predeclared identifiers are defined in the [builtin](https://golang.org/pkg/builtin/) package.
All Go's predeclared identifiers are defined in the [builtin](https://golang.org/pkg/builtin/) package.

## Type Conversions
```go
Expand Down Expand Up @@ -308,7 +312,7 @@ func main() {
}
for { // you can omit the condition ~ while (true)
}

// use break/continue on current loop
// use break/continue with label on outer loop
here:
Expand Down Expand Up @@ -783,4 +787,78 @@ func main() {
// }
```

## Generics

Generics in Go, introduced in Go 1.18, allow for writing functions and types that are abstracted over types, which can be specified when the function or type is used.

### Type Parameters

A generic function with **type parameters** allows the function to operate on different types without specifying the exact types it works with in the function definition.

```go
// Here, T is a type parameter that can be any type.
func GenericPrint[T any](value T) {
fmt.Println(value)
}

func main() {
GenericPrint("Hello, Generics!") // for string
GenericPrint(123) // for int
GenericPrint(3.14) // for float64
}
```

### Constraints

**Constraints** are used in generics to specify the capabilities required of the type parameters, like needing them to implement a certain interface or be a certain kind of type.

```go
// Deduplicate takes a slice of any comparable type and returns a slice with unique elements.
func Deduplicate[T comparable](slice []T) []T {
unique := make(map[T]bool)
var result []T

for _, v := range slice {
if _, exists := unique[v]; !exists {
unique[v] = true
result = append(result, v)
}
}

return result
}

func main() {
ints := []int{1, 2, 2, 3, 4, 4, 4, 5}
fmt.Println("Unique ints:", Deduplicate(ints)) // for ints

strings := []string{"apple", "orange", "apple", "banana", "orange"}
fmt.Println("Unique strings:", Deduplicate(strings)) // for strings
}
```

### Generic Types

**Generic types** allow the creation of data structures that can work with any type, such as a generic container, without specifying the exact type it contains in the definition.

```go
// GenericSlice is a generic type that can hold a slice of any type.
type GenericSlice[T any] []T

// AppendValue appends a value with any type to the slice.
func (s *GenericSlice[T]) AppendValue(value T) {
*s = append(*s, value)
}

func main() {
intSlice := GenericSlice[int]{}
intSlice.AppendValue(1)
intSlice.AppendValue(2)
fmt.Println("Slice of ints:", intSlice) // for GenericSlice[int]

stringSlice := GenericSlice[string]{}
stringSlice.AppendValue("Hello")
stringSlice.AppendValue("Generics")
fmt.Println("Slice of strings:", stringSlice) // for GenericSlice[string]
}
```