英文:
Working with returned value in [for] loop
问题
我有使用C#的经验,可以处理用户的命令,代码如下:
string command;
while ((command = GetCommandFromUser()) != EXIT_COMMAND)
ProcessCommand(command);
这段简单的代码允许我从用户那里获取命令(从控制台或类似的地方),并对其进行处理。
但是在Go语言中,我只有以下代码:
var command string
for command = GetCommandFromUser(); command != ExitCommand; command = GetCommandFromUser() {
ProcessCommand(command)
}
我能简化它吗?
英文:
I have an experience with C#, where I can work with commands from user like this:
string command;
while ( (command = GetCommandFromUser()) != EXIT_COMMAND )
ProcessCommand(command);
This simple code allows me get command from user (from Console or something like this) and process it.
But in Go
I have only this code:
var command string
for command = GetCommandFromUser(); command != ExitCommand; command = GetCommandFromUser() {
ProcessCommand(command)
}
Can I do it simply?
答案1
得分: 2
for {
command := GetCommandFromUser()
if command == ExitCommand {
break
}
ProcessCommand(command)
}
for {
command := GetCommandFromUser()
if command == ExitCommand {
break
}
ProcessCommand(command)
}
for {
command := GetCommandFromUser()
if command == ExitCommand {
break
}
ProcessCommand(command)
}
英文:
for {
command := GetCommandFromUser()
if command == ExitCommand {
break
}
ProcessCommand(command)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论