Created
May 24, 2024 15:08
-
-
Save carolinux/37195ff95fc06221c3158d75e71bb365 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// 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() | |
} |
A goroutine is not an OS thread. However, if a go routine is blocking, new OS threads will be started to run other coroutines.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wonder, line 22, this object: curr.next = &List[T]{nil, x} -- it has lifetime until it's magically garbage collected?