Skip to content

Instantly share code, notes, and snippets.

@hellojukay
Created August 14, 2021 03:56
Show Gist options
  • Save hellojukay/1cdc675939d8fcf920dabeb9c4e11637 to your computer and use it in GitHub Desktop.
Save hellojukay/1cdc675939d8fcf920dabeb9c4e11637 to your computer and use it in GitHub Desktop.
Use golang to implement linked list in functional style
package main
type Node struct {
Val int
Next interface{}
}
func create() interface{} {
return nil
}
func insert(list interface{}, n int) Node {
return Node{
Val: n,
Next: list,
}
}
func loop(list interface{}) {
if list == nil {
return
}
var node = list.(Node)
println(node.Val)
loop((node.Next))
}
func main() {
var list = create()
for i := 0; i < 10; i++ {
list = insert(list, i+2)
}
loop(list)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment