100 lines
2.2 KiB
Go
100 lines
2.2 KiB
Go
// code form https://medium.com/backendarmy/linked-lists-in-go-f7a7b27a03b9
|
|
// this code is use to test treesiter parser
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
type song struct {
|
|
name string
|
|
artist string
|
|
next *song
|
|
}
|
|
|
|
type playlist struct {
|
|
name string
|
|
head *song
|
|
nowPlaying *song
|
|
}
|
|
|
|
func createPlaylist(name string) *playlist {
|
|
return &playlist{
|
|
name: name,
|
|
}
|
|
}
|
|
|
|
func (p *playlist) addSong(name, artist string) error {
|
|
s := &song{
|
|
name: name,
|
|
artist: artist,
|
|
}
|
|
if p.head == nil {
|
|
p.head = s
|
|
} else {
|
|
currentNode := p.head
|
|
for currentNode.next != nil {
|
|
currentNode = currentNode.next
|
|
}
|
|
currentNode.next = s
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *playlist) showAllSongs() error {
|
|
currentNode := p.head
|
|
if currentNode == nil {
|
|
fmt.Println("Playlist is empty.")
|
|
return nil
|
|
}
|
|
fmt.Printf("%+v\n", *currentNode)
|
|
for currentNode.next != nil {
|
|
currentNode = currentNode.next
|
|
fmt.Printf("%+v\n", *currentNode)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *playlist) startPlaying() *song {
|
|
p.nowPlaying = p.head
|
|
return p.nowPlaying
|
|
}
|
|
|
|
func (p *playlist) nextSong() *song {
|
|
p.nowPlaying = p.nowPlaying.next
|
|
return p.nowPlaying
|
|
}
|
|
|
|
func (p *playlist) NextSong() *song { // exported
|
|
p.nowPlaying = p.nowPlaying.next
|
|
return p.nowPlaying
|
|
}
|
|
|
|
func play() {
|
|
playlistName := "myplaylist"
|
|
myPlaylist := createPlaylist(playlistName)
|
|
fmt.Println("Created playlist")
|
|
fmt.Println()
|
|
|
|
fmt.Print("Adding songs to the playlist...\n\n")
|
|
myPlaylist.addSong("Ophelia", "The Lumineers")
|
|
myPlaylist.addSong("Shape of you", "Ed Sheeran")
|
|
myPlaylist.addSong("Stubborn Love", "The Lumineers")
|
|
myPlaylist.addSong("Feels", "Calvin Harris")
|
|
fmt.Println("Showing all songs in playlist...")
|
|
myPlaylist.showAllSongs()
|
|
fmt.Println()
|
|
|
|
myPlaylist.startPlaying()
|
|
fmt.Printf("Now playing: %s by %s\n", myPlaylist.nowPlaying.name, myPlaylist.nowPlaying.artist)
|
|
fmt.Println()
|
|
|
|
myPlaylist.nextSong()
|
|
fmt.Println("Changing next song...")
|
|
fmt.Printf("Now playing: %s by %s\n", myPlaylist.nowPlaying.name, myPlaylist.nowPlaying.artist)
|
|
fmt.Println()
|
|
myPlaylist.nextSong()
|
|
fmt.Println("Changing next song...")
|
|
fmt.Printf("Now playing: %s by %s\n", myPlaylist.nowPlaying.name, myPlaylist.nowPlaying.artist)
|
|
|
|
}
|