Skip to content

Instantly share code, notes, and snippets.

@carolinux
Created May 24, 2024 15:08
Show Gist options
  • Save carolinux/37195ff95fc06221c3158d75e71bb365 to your computer and use it in GitHub Desktop.
Save carolinux/37195ff95fc06221c3158d75e71bb365 to your computer and use it in GitHub Desktop.
package main
// https://go.dev/tour/generics/2
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]) lappend(x T){
curr := l
for {
if curr.next == nil{
break
}
curr = curr.next
}
curr.next = &List[T]{nil, x}
}
func (l *List[T]) print(){
curr := l
for {
fmt.Println(curr.val)
if curr.next == nil{
break
}
curr = curr.next
}
}
func main() {
head:= List[int]{nil, 3}
head.lappend(4)
head.lappend(5)
head.lappend(3)
head.print()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment