douran9707 2017-02-08 02:31
浏览 18
已采纳

没有匿名数组吗?

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 2017-02-08 02:36
    关注

    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},
            },
        },
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!