2 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
創(chuàng)建電影片段。 將每部電影附加到切片。要打印,請(qǐng)覆蓋切片并打印每部電影。
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)
}
邏輯可以包含在一個(gè)類(lèi)型中:
// 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()
}

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊
創(chuàng)建結(jié)構(gòu)新的電影數(shù)組
創(chuàng)建全局變量 Movies 來(lái)保存數(shù)據(jù)
在 func main 上調(diào)用變量
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)
}
- 2 回答
- 0 關(guān)注
- 175 瀏覽
添加回答
舉報(bào)