如何在结构体中添加元素

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

How to add an element in a Struct

问题

我有一个在Golang中的结构体:

type User struct {
	Username        string `json:"username"`
	Password        string `json:"password"`
	ConfirmPassword string `json:"confirmpassword"`
	Firstname       string `json:"firstname"`
	Lastname        string `json:"lastname"`
	Day             int    `json:"day"`
	Month           int    `json:"month"`
	Year            int    `json:"year"`
}

我创建了以下的种子数据:

var users = []User{
	{Username: "xGiovanni", Password: "nike4545", ConfirmPassword: "nike4545", Firstname: "Giovanni", Lastname: "Mosquera", Day: 27, Month: 07, Year: 2001},
	{Username: "Juseros9", Password: "contraseña", ConfirmPassword: "contraseña", Firstname: "Sebastián", Lastname: "Rodriguez", Day: 16, Month: 11, Year: 2002},
}

我的问题是,如何以相同的JSON格式向users变量添加一个新元素?
例如,添加以下内容:

{Username: username, Password: password, ConfirmPassword: confirm_password, Firstname: firstname, Lastname: lastname, Day: strconv.Atoi(day), Month: strconv.Atoi(month), Year: strconv.Atoi(year)}
英文:

I have this Struct in Golang:

type User struct {
	Username        string `json:"username"`
	Password        string `json:"password"`
	ConfirmPassword string `json:"confirmpassword"`
	Firstname       string `json:"firstname"`
	Lastname        string `json:"lastname"`
	Day             int    `json:"day"`
	Month           int    `json:"month"`
	Year            int    `json:"year"`
}

And i made this seeder:

var users = []User{
	{Username: "xGiovanni", Password: "nike4545", ConfirmPassword: "nike4545", Firstname: "Giovanni", Lastname: "Mosquera", Day: 27, Month: 07, Year: 2001},
	{Username: "Juseros9", Password: "contraseña", ConfirmPassword: "contraseña", Firstname: "Sebastián", Lastname: "Rodriguez", Day: 16, Month: 11, Year: 2002},
}

And my question is, ¿How can i add a new element with the same JSON format to the users var?
For example... add this:

{Username: username, Password: password, ConfirmPassword: confirm_password, Firstname: firstname, Lastname: lastname, Day: strconv.Atoi(day), Month: strconv.Atoi(month), Year: strconv.Atoi(year)}

答案1

得分: 2

append()函数接受一个类型为[]T的切片,然后接受一个可变数量的与切片成员类型T相同的值。

解决方法是使用slice...

newUser := []User{
    {Username: "Juseros10", Password: "contraseña", ConfirmPassword: "contraseña", Firstname: "Sebastián", Lastname: "Rodriguez", Day: 16, Month: 11, Year: 2003},
}

users = append(users, newUser...)
英文:

append() takes a slice of type []T, and then a variable number of values of the type of the slice member T.

The solution to this is to use the slice....

newUser := []User{
	{Username: "Juseros10", Password: "contraseña", ConfirmPassword: "contraseña", Firstname: "Sebastián", Lastname: "Rodriguez", Day: 16, Month: 11, Year: 2003},
}

users = append(users, newUser...)

答案2

得分: 2

你可以使用append()方法将新元素添加到你的用户切片中,以下是一个实现相同逻辑的简单程序:

package main

import (
	"fmt"
)

type User struct {
	Username        string `json:"username"`
	Password        string `json:"password"`
	ConfirmPassword string `json:"confirmpassword"`
	Firstname       string `json:"firstname"`
	Lastname        string `json:"lastname"`
	Day             int    `json:"day"`
	Month           int    `json:"month"`
	Year            int    `json:"year"`
}

func main() {

	var users = []User{
		{Username: "xGiovanni", Password: "nike4545", ConfirmPassword: "nike4545", Firstname: "Giovanni", Lastname: "Mosquera", Day: 27, Month: 07, Year: 2001},
		{Username: "Juseros9", Password: "contraseña", ConfirmPassword: "contraseña", Firstname: "Sebastián", Lastname: "Rodriguez", Day: 16, Month: 11, Year: 2002},
	}

	users = append(users, User{Username: "Luduvico", Password: "Einuadi", ConfirmPassword: "Einuadi", Firstname: "Ludovico", Lastname: "Einudi", Day: 25, Month: 07, Year: 1971})
	fmt.Println(users)
}

输出结果:

[{xGiovanni nike4545 nike4545 Giovanni Mosquera 27 7 2001} {Juseros9 contraseña contraseña Sebastián Rodriguez 16 11 2002} {Luduvico Einuadi Einuadi Ludovico Einudi 25 7 1971}]
英文:

You can make use of the append() method to add new element into your users slice,here is a simple program for the same logic:

package main

import (
	"fmt"
)

type User struct {
	Username        string `json:"username"`
	Password        string `json:"password"`
	ConfirmPassword string `json:"confirmpassword"`
	Firstname       string `json:"firstname"`
	Lastname        string `json:"lastname"`
	Day             int    `json:"day"`
	Month           int    `json:"month"`
	Year            int    `json:"year"`
}

func main() {

	var users = []User{
		{Username: "xGiovanni", Password: "nike4545", ConfirmPassword: "nike4545", Firstname: "Giovanni", Lastname: "Mosquera", Day: 27, Month: 07, Year: 2001},
		{Username: "Juseros9", Password: "contraseña", ConfirmPassword: "contraseña", Firstname: "Sebastián", Lastname: "Rodriguez", Day: 16, Month: 11, Year: 2002},
	}

	users = append(users, User{Username: "Luduvico", Password: "Einuadi", ConfirmPassword: "Einuadi", Firstname: "Ludovico", Lastname: "Einudi", Day: 25, Month: 07, Year: 1971})
	fmt.Println(users)
}

Output:

[{xGiovanni nike4545 nike4545 Giovanni Mosquera 27 7 2001} {Juseros9 contraseña contraseña Sebastián Rodriguez 16 11 2002} {Luduvico Einuadi Einuadi Ludovico Einudi 25 7 1971}]

答案3

得分: 0

我已经声明了一个变量a,用于追加数据。你也可以添加更多的变量来进行追加。

package main

import "fmt"

type User struct {
    Title, Body string
}

func main() {
    a := User{Title: "New title", Body: "New Body"}

    var new []User
    new = append(new, a)

    fmt.Println(new[0])
}

我已经将代码翻译成中文,你可以查看上面的代码。

英文:

I have declared a variable a to append data into it. You can add more variables also to append.

package main

import "fmt"

type User struct {
	Title, Body string
}

func main() {
	a := User{Title: "New title", Body: "New Body"}

	var new []User
	new = append(new, a)

	fmt.Println(new[0])
}

huangapple
  • 本文由 发表于 2021年9月13日 08:14:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/69156086.html
匿名

发表评论

匿名网友

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

确定