英文:
Using Go to avoid duplicates from existing array data when appending slices with user input
问题
请原谅我所有的评论,我正在学习,这使我保持专注。我正在寻找一个更好的解决方案来处理我在底部附近的条件语句,并提供其余的内容以供参考。我尝试了建议的循环注释,但仍然没有得到正确的结果,如下所示:
// 定义主包文件
package main
// 导入用户I/O和字符串操作包的格式化功能
// 如果只导入一个包,括号是不需要的
import (
"fmt"
"strings"
)
// 定义独立应用程序的主函数
func main() {
// 初始化一个字符串数组
options := []string{"lemon", "orange", "strawberry", "banana", "grape"}
// 在数组中创建一个切片来指定喜爱的水果
favFruits := options[0:3]
fmt.Println(options)
// 初始化用户输入的变量
var fruit string
fmt.Print("Favorite Fruit Not Listed: ")
fmt.Scan(&fruit)
// 将响应转换为小写
fruitConv := strings.ToLower(fruit)
// 条件语句示例
for _, pick := range options {
if pick == fruitConv {
fmt.Println("You were supposed to pick something else")
break
} else {
fmt.Println("Response Accepted!")
break
}
break
}
// 将用户输入的喜爱水果添加到切片数组中
favFruits = append(favFruits, fruitConv)
fmt.Print(favFruits)
}
英文:
Forgive all my comments, I'm learning and that keeps me straight. Looking for a better solution to my conditional statement near the bottom and provided the rest for context. Tried out the loop comment suggested, but still not getting the right result as written here
// define the main package file
package main
//importing formatting features for the user I/O and string manipulation packages
//Parenthesis not needed if only importing 1 package
import (
"fmt"
"strings"
)
// defining the main function of the standalone app
func main() {
//initializing a string array
options := []string{"lemon", "orange", "strawberry", "banana", "grape"}
//creating a slice in the array to designate favorites
favFruits := options[0:3]
fmt.Println(options)
//initializing variable for user input
var fruit string
fmt.Print("Favorite Fruit Not Listed: ")
fmt.Scan(&fruit)
//convert response to lowercase
fruitConv := strings.ToLower(fruit)
//conditional statement example
for _, pick := range options {
if pick == fruitConv {
fmt.Println("You were supposed to pick something else")
break
} else {
fmt.Println("Response Accepted!")
break
}
break
}
//adding user inputed favorite fruit to the sliced array
favFruits = append(favFruits, fruitConv)
fmt.Print(favFruits)
}
答案1
得分: 0
我明白了。如果你去官方的Go packages
部分,你会找到slices
包。参考链接:slices
在slices
包中有一个名为contains
的函数,你可以使用这个函数。为了简单起见,我创建了一个与你的场景匹配的示例。
func main() {
//初始化一个字符串数组
options := []string{"lemon", "orange", "strawberry", "banana", "grape"}
//假设水果是用户输入
fruit := "lemon"
//条件语句示例
if fruit == "coconut" || fruit == "coconuts" {
fmt.Println("Coconuts are nasty (except on your hair/skin)")
//**如果数组在服务器端增长,应该有简化这段代码的方法**
} else if slices.Contains(options, fruit) {
fmt.Println("You were supposed to pick something different...")
}
}
这是工作链接:https://goplay.tools/snippet/bzSm_biR4Vr
**注意:**由于在你的场景中有很多这样的包含检查,我建议使用map
而不是切片。如果你深入研究切片的包含函数,你会了解到其中的原理。
英文:
I got your point. If you go to official Go packages
section you will get slices
package over there. Ref: slices
There is one function named contains
inside the slices
package, you can make use of that function. For your simplicity, I have created one example matching with your scenario.
func main() {
//initializing a string array
options := []string{"lemon", "orange", "strawberry", "banana", "grape"}
// assuming fruit as user input
fruit := "lemon"
//conditional statement example
if fruit == "coconut" || fruit == "coconuts" {
fmt.Println("Coconuts are nasty (except on your hair/skin)")
//**there has to be a way to simplify this code if the array grows server-side or something**
} else if slices.Contains(options, fruit) {
fmt.Println("You were supposed to pick something different...")
}
}
Here is the working link :- https://goplay.tools/snippet/bzSm_biR4Vr
Note: Since in your scenario there is lot of such contain checks, I would recommend to use map
instead of slice. Internally if you deep dive in conatins function of slice you will get to know.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论