英文:
try to fill slice with struct and method in golang
问题
我想创建一个名为movie
的结构体,它有title
和genre
属性,数据类型为string
,然后有duration
和year
属性,数据类型为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)
}
我得到的结果是:
希望能帮到你。提前感谢你的帮助。
英文:
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:
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 :
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()
}
答案2
得分: 1
- 创建一个名为Movies的结构体数组
- 创建一个名为Movies的全局变量来保存数据
- 在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)
}
英文:
- create struct new array of Movies
- create global variable Movies to save data
- 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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论