douran9707
douran9707
2017-02-08 02:31

没有匿名数组吗?

已采纳

Instead of doing:

var adapters[]LogicAdapter
adapter1 := &ExampleAdapter{0.9}
adapter2 := &ExampleAdapter{0.8}
adapters = append(adapters, adapter1, adapter2)
bot := ChatterBot{"Charlie", MultiLogicAdapter{adapters}}

I tried:

bot := ChatterBot{
    "Charlie", 
    MultiLogicAdapter{
        []LogicAdapter{
            &ExampleAdapter{0.9},
            &ExampleAdapter{0.8}
        }
    }
}

But why this won't work? I cannot see any problems with the syntax, it's like I were creating a new slice and putting it into there. Here are the errors:

./main.go:16: syntax error: unexpected semicolon or newline, expecting comma or }
./main.go:21: syntax error: unexpected semicolon or newline, expecting comma or }
./main.go:22: syntax error: unexpected semicolon or newline, expecting comma or }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongyingjiu0669 dongyingjiu0669 4年前

    You just need a comma at the end of &ExampleAdapter{0.8} and after the closing braces that end the other elements.. Go syntax is pretty strict. If you don't have the closing brace on the same line, you need to end the line with a comma, even if it's the last element. That's what the error message is saying. Your code should look like this:

    bot := ChatterBot{
        "Charlie",
        MultiLogicAdapter{
            []LogicAdapter{
                &ExampleAdapter{0.9},
                &ExampleAdapter{0.8},
            },
        },
    }
    
    点赞 评论 复制链接分享

相关推荐