英文:
How to print all the values of a variable outside a loop?
问题
在这段代码中,我有一个函数,其中我查找用户的姓名。它在循环内找到它们并打印出所有的姓名,但是当我在循环外返回s
变量时,它只打印出第一个姓名。它不会打印出所有的姓名。
尽管我将s
变量附加到另一个变量中,但仍然不起作用。
type Signup struct {
Names string
Emails string
}
func FindNames() string {
db := Connect()
var s string
rows, err := db.Query("select names from Signup")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var signup Signup
err := rows.Scan(&signup.Names)
if err != nil {
log.Fatal(err)
}
// s = signup.Names
// fmt.Println(s)
s = fmt.Sprintf("%s", signup.Names)
}
return s
}
英文:
In this code, I have a function in which I find the users' names. It finds them and prints all the names inside the loop but when I return the s
variable outside the loop, it prints only the first name. It does not print all the names.
Although I append s
variable in another variable also but still does not work.
type Signup struct {
Names string
Emails string
}
func FindNames() string {
db := Connect()
var s string
rows, err := db.Query("select names from Signup")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var signup Signup
err := rows.Scan(&signup.Names)
if err != nil {
log.Fatal(err)
}
// s = signup.Names
// fmt.Println(s)
s = fmt.Sprintf("%s", signup.Names)
}
return s
}
答案1
得分: 1
在循环遍历时,将结果放入列表中,然后对结果执行一些操作是有意义的。或者可以使用字符串构建器(因为它更快)。
使用列表的示例
type Signup struct {
Names string
Emails string
}
func FindNames() string {
db := Connect()
var s string
rows, err := db.Query("select names from Signup")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var names []string
for rows.Next() {
var signup Signup
err := rows.Scan(&signup.Names)
if err != nil {
log.Fatal(err)
}
names = append(names, signup.Names)
}
return strings.Join(names, "\n") // 使用换行符(\n)打印多行。
}
字符串构建器
func FindNames() string {
db := Connect()
var s string
rows, err := db.Query("select names from Signup")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var sb strings.Builder
for rows.Next() {
var signup Signup
err := rows.Scan(&signup.Names)
if err != nil {
log.Fatal(err)
}
sb.WriteString(signup.Names)
sb.WriteRune('\n')
}
return sb.String()
}
英文:
When looping over stuff it'd make sense to put the results in a list, then do some actions on the results. Or to use a stringbuilder (as it's faster).
Example with list
type Signup struct {
Names string
Emails string
}
func FindNames() string {
db := Connect()
var s string
rows, err := db.Query("select names from Signup")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var names []string
for rows.Next() {
var signup Signup
err := rows.Scan(&signup.Names)
if err != nil {
log.Fatal(err)
}
names = append(names, signup.Names)
}
return strings.Join(names, "\n") // newline (\n) character to print multiple lines.
}
Stringbuilder
func FindNames() string {
db := Connect()
var s string
rows, err := db.Query("select names from Signup")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var sb strings.Builder
for rows.Next() {
var signup Signup
err := rows.Scan(&signup.Names)
if err != nil {
log.Fatal(err)
}
sb.WriteString(signup.Names)
sb.WriteRune('\n')
}
return sb.String()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论