Struct pointing to itself from blog.golang maps tutorial

Studying example from https://blog.golang.org/maps on maps(code is below)

Questions I have
1)I don’t understand the struct Node… How can Next be pointed to itself?
2)How would you initialize this struct with data?? I can’t even run to exam how this works…

Any help would be appreciated. thank you!

package main
import "fmt"

func main() {
    type Node struct {
        Next  *Node
        Value interface{}
    }

    var first *Node
    visited := make(map[*Node]bool)

    for n := first; n != nil; n = n.Next {
        if visited[n] {
            fmt.Println("cycle detected")
            break
        }

        visited[n] = true
        fmt.Println("nada")
        fmt.Println(n.Value)
    }
}

A Node can point to another Node:

var third = &Node{nil, "3rd"}
var second = &Node{third, "2nd"}
var first = &Node{second, "1st"}

Note that I construct the chain of nodes beginning with the last one which has no Next node.

For a working example see https://play.golang.org/p/epsTrOpVXnP.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.