Go程序编译错误

huangapple go评论84阅读模式
英文:

Compilation error in go program

问题

这是代码,我使用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-1] == "[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)	
			
			nod.links[field] = 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

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

英文:

here is the code and i use gccgo for compilation. this for a graph based organizer. I don't need advise on graph algorithms.

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")
}

and this is the error I got, I tried my best but I cannot resolve.

$ 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

I do not understand where I need to use the semi-colon and why.

答案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)

否则会出现错误。

英文:

The problem, I think, may be in func new_node:

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

Should be

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

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

Also, I have a feeling that, in func nodeFromString (line 93-ish):

append(nod.links[field], link)

Should be:

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

Otherwise you'll get an error.

huangapple
  • 本文由 发表于 2013年10月14日 20:26:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/19360239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定