0

这是代码,我使用 gccgo 进行编译。这适用于基于图表的组织者。我不需要关于图形算法的建议。

package element

import (
        "fmt"
        "strings"
        "io"
        "strconv"
)

type Node struct {
    id   int
    name string
    props map[string]string
    links map[string][]*Node
}

var names = make(map[string]int , 8)
var nodes = make(map[string][]*Node , 8)


//========functions================
func new_node(Id int) *Node {
    return  &Node( Id, " ", nil, nil) 
}

func getNode_byId(nodes []*Node, id int) *Node {    
    for _, node := range nodes{
        if node.id == id {
            return node
        }
    }
    return nil
}

func addNode(store string, node *Node) {
    nodes[store] = append(nodes[store], node)
}

func addLinkToNode(node, link *Node, property string) {
    node.links[property] = append(node.links[property], link)
}

func nodeFromString(str string, typ string) {

    lines := strings.Split(str, "\n")
    lcount := len(lines)

    if lines[0] == "[begin]" && lines[lcount] == "[end]" {
        fmt.Println("common dude! something wrong with ur string")
        return
    }

    fields  := strings.Fields(lines[1])
    id , _  := strconv.Atoi(fields[1])
    nod     := getNode_byId(nodes[typ], id )

    if nod == nil { nod = new_node(id) }
    addNode(typ, nod)

    nod.name = typ

    lines = lines[2:]
    ind :=0
    for index, line := range lines {
        fields := strings.Fields(line)
        if field := fields[0]; field[0] != '-' {
            ind = index
            break
        }

        nod.props[fields[0]] = fields[1]
    }

    lines = lines[ind:] 
    for index, line := range lines {
        if line[0]!= '+' {
            ind = index
            break
        }

        pivot := strings.Index(line, " ")
        field := line[0:pivot]
        fields := strings.Split(line[pivot:], ",")      

    for _, value := range fields {
        id, _ := strconv.Atoi(strings.TrimSpace(value))
        var link *Node = getNode_byId(nodes[typ], id)
        if link == nil { link = new_node(id) }  
        addNode(typ, link)  

        append(nod.links[field], link ) 
    }
}
}


func equal_byId( nodeA, nodeB Node) bool {
    return (nodeA.id == nodeB.id)
}

func equal_byProp( nodeA, nodeB Node, property string) bool {
    return (nodeA.props[property] == nodeB.props[property])
}

//========methods on node==========
func (node Node) IsEqual_byId( comparand Node ) bool {
    return equal_byId(node, comparand)
}

func (node Node) IsEqual_byProp( comparand Node, property string ) bool {
    return equal_byProp(node, comparand, property)
}

func (node *Node) addLink (property string, link *Node){
    addLinkToNode( node, link, property)
}

//===================
func main() {
        fmt.Println("hello world")
}

这是我得到的错误,我尽力了,但我无法解决。

$ gccgo elements.go
elements.go:23:19: error: expected ‘)’
elements.go:23:34: error: expected ‘;’ or ‘}’ or newline
elements.go:23:2: error: too many values in return statement
elements.go:91:4: error: value computed is not used

我不明白我需要在哪里使用分号以及为什么。

4

1 回答 1

3

我认为问题可能出在func new_node

return  &Node( Id, " ", nil, nil)

应该

return  &Node{Id, " ", nil, nil}

http://golang.org/ref/spec#Composite_literals

另外,我有一种感觉,在func nodeFromString(第 93 行):

append(nod.links[field], link)

应该:

nod.links[field] = append(nod.links[field], link)

否则你会得到一个错误。

于 2013-10-14T12:36:37.383 回答