Created
March 21, 2022 17:27
-
-
Save prb112/9b4454367f412184eaec129efea825d8 to your computer and use it in GitHub Desktop.
https://go.dev/tour/generics/2 and worthy of a reference https://drgarcia1986.medium.com/playing-with-go-and-generics-bba6961b14a0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
// List represents a singly-linked list that holds | |
// values of any type. | |
type List[T any] struct { | |
next *List[T] | |
val T | |
} | |
func (l *List[T]) ShowList() { | |
current := l | |
for current != nil { | |
fmt.Println("The value is: ", current.val) | |
current = current.next | |
} | |
} | |
func main() { | |
n3 := List[int]{val: 3} | |
n2 := List[int]{val: 2, next: &n3} | |
n1 := List[int]{val: 1, next: &n2} | |
n1.ShowList() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the Tour isn't a hundred percent clear, Diego's site elaborates in depth.