如何在循环外打印变量的所有值?

huangapple go评论105阅读模式
英文:

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()
}

huangapple
  • 本文由 发表于 2022年9月6日 14:49:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/73617777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定