尝试使用结构体和方法在Go语言中填充切片。

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

try to fill slice with struct and method in golang

问题

我想创建一个名为movie的结构体,它有titlegenre属性,数据类型为string,然后有durationyear属性,数据类型为integer。然后创建一个名为addDataFilm的函数,将结构体中的数据对象添加到dataFilm切片中,然后显示数据。

这是我的代码:

type movie struct {
	title, genre   string
	duration, year int
}

func (m movie) addDataFilm(title string, genre string, duration int, year int, dataFilm *[]string) {
	var d = strconv.Itoa(m.duration)
	var y = strconv.Itoa(m.year)
	*dataFilm = append(*dataFilm, m.title, m.genre, d, y)
}

func main(){
        var dataFim = []string{}
	var dd = movie{}

	dd.addDataFilm("LOTR", "action", 120, 1999, &dataFim)
	dd.addDataFilm("Avanger", "action", 120, 2004, &dataFim)
	dd.addDataFilm("Spiderman", "action", 120, 2004, &dataFim)
	dd.addDataFilm("Juon", "horror", 120, 2004, &dataFim)

	fmt.Println(dataFim)
}

我得到的结果是:

尝试使用结构体和方法在Go语言中填充切片。

希望能帮到你。提前感谢你的帮助。

英文:

I want to create a struct movie with the property title and genre data type string, then duration and year data type integer
then create a function with the name addDataFilm to add the data object from the struct to the dataFilm slice, then display the data:

尝试使用结构体和方法在Go语言中填充切片。

this is my code :

type movie struct {
	title, genre   string
	duration, year int
}

func (m movie) addDataFilm(title string, genre string, duration int, year int, dataFilm *[]string) {
	var d = strconv.Itoa(m.duration)
	var y = strconv.Itoa(m.year)
	*dataFilm = append(*dataFilm, m.title, m.genre, d, y)
}

func main(){
        var dataFim = []string{}
	var dd = movie{}

	dd.addDataFilm("LOTR", "action", 120, 1999, &dataFim)
	dd.addDataFilm("Avanger", "action", 120, 2004, &dataFim)
	dd.addDataFilm("Spiderman", "action", 120, 2004, &dataFim)
	dd.addDataFilm("Juon", "horror", 120, 2004, &dataFim)

	fmt.Println(dataFim)
}

all i got is :

尝试使用结构体和方法在Go语言中填充切片。

any help will be appreciated. thank you in advance

答案1

得分: 2

创建一个电影的切片。将每个电影添加到切片中。使用range遍历切片并打印每个电影。

var movies []*movie
movies = append(movies, &movie{"LOTR", "action", 120, 1999})
movies = append(movies, &movie{"Avanger", "action", 120, 2004})
movies = append(movies, &movie{"Spiderman", "action", 120, 2004})
movies = append(movies, &movie{"Juon", "horror", 120, 2004})
for i, m := range movies {
    fmt.Printf("%d. Title: %s\n   Genre: %s\n   Duration: %d\n   Year: %d\n\n", i+1, m.title, m.genre, m.duration, m.year)
}

在playground上运行程序:点击这里

可以将逻辑封装在一个类型中:

// dataFilms 存储多个电影的数据。
type dataFilms []*movie

func (df *dataFilms) add(title string, genre string, duration int, year int) {
    *df = append(*df, &movie{title, genre, duration, year})
}

func (df dataFilms) print() {
    for i, m := range df {
        fmt.Printf("%d. Title: %s\n   Genre: %s\n   Duration: %d\n   Year: %d\n\n", i+1, m.title, m.genre, m.duration, m.year)
    }
}

func main() {
    var df dataFilms
    df.add("LOTR", "action", 120, 1999)
    df.add("Avanger", "action", 120, 2004)
    df.add("Spiderman", "action", 120, 2004)
    df.add("Juon", "horror", 120, 2004)
    df.print()
}

在playground上运行程序:点击这里

英文:

Create a slice of movies. Append each movie to the slice. To print, range over the slice and print each movie.

var movies []*movie
movies = append(movies, &movie{"LOTR", "action", 120, 1999})
movies = append(movies, &movie{"Avanger", "action", 120, 2004})
movies = append(movies, &movie{"Spiderman", "action", 120, 2004})
movies = append(movies, &movie{"Juon", "horror", 120, 2004})
for i, m := range movies {
	fmt.Printf("%d. Title: %s\n   Genre: %s\n   Duration: %d\n   Year: %d\n\n", i+1, m.title, m.genre, m.duration, m.year)
}

Run the program on the playground.

The logic can be wrapped up in a type:

// dataFilms stores data for multiple films.
type dataFilms []*movie

func (df *dataFilms) add(title string, genre string, duration int, year int) {
	*df = append(*df, &movie{title, genre, duration, year})
}

func (df dataFilms) print() {
	for i, m := range df {
		fmt.Printf("%d. Title: %s\n   Genre: %s\n   Duration: %d\n   Year: %d\n\n", i+1, m.title, m.genre, m.duration, m.year)
	}
}

func main() {
	var df dataFilms
	df.add("LOTR", "action", 120, 1999)
	df.add("Avanger", "action", 120, 2004)
	df.add("Spiderman", "action", 120, 2004)
	df.add("Juon", "horror", 120, 2004)
	df.print()
}

Run it on the playground.

答案2

得分: 1

  1. 创建一个名为Movies的结构体数组
  2. 创建一个名为Movies的全局变量来保存数据
  3. 在main函数中调用该变量
type Movie struct {
	Title    string
	Genre    string
	Duration int
	Year     int
}

type Movies []Movie

var dest Movies

func addDataFilm(title string, genre string, duration int, year int) Movies {

	dest = append(dest, Movie{
		Title:    title,
		Genre:    genre,
		Duration: duration,
		Year:     year,
	})

	return dest
}

func TestNumberToAlphabet(t *testing.T) {
	addDataFilm("LOTR", "action", 120, 1999)
	addDataFilm("Avanger", "action", 120, 2004)
	addDataFilm("Spiderman", "action", 120, 2004)
	addDataFilm("Juon", "horror", 120, 2004)

	fmt.Println(dest)

}
英文:
  1. create struct new array of Movies
  2. create global variable Movies to save data
  3. call variable on func main
type Movie struct {
	Title    string
	Genre    string
	Duration int
	Year     int
}

type Movies []Movie

var dest Movies

func addDataFilm(title string, genre string, duration int, year int) Movies {

	dest = append(dest, Movie{
		Title:    title,
		Genre:    genre,
		Duration: duration,
		Year:     year,
	})

	return dest
}

func TestNumberToAlphabet(t *testing.T) {
	addDataFilm("LOTR", "action", 120, 1999)
	addDataFilm("Avanger", "action", 120, 2004)
	addDataFilm("Spiderman", "action", 120, 2004)
	addDataFilm("Juon", "horror", 120, 2004)

	fmt.Println(dest)

}

huangapple
  • 本文由 发表于 2022年3月30日 10:50:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/71670948.html
匿名

发表评论

匿名网友

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

确定