Skip to content

Instantly share code, notes, and snippets.

@valkheim
Created April 23, 2018 01:20
Show Gist options
  • Save valkheim/a2f260c8e56953102702fee5dedf5bec to your computer and use it in GitHub Desktop.
Save valkheim/a2f260c8e56953102702fee5dedf5bec to your computer and use it in GitHub Desktop.
package main
import (
"errors"
"fmt"
)
type queue struct {
Type string
Q []interface{}
init bool
}
func (q *queue) Add(item interface{}) error {
if !q.init {
q.Type = fmt.Sprintf("%T", item)
q.Q = append(q.Q, item)
q.init = true
return nil
} else if fmt.Sprintf("%T", item) != q.Type {
return errors.New("Wrong type! Queue is of type " + q.Type + ". Given type: " + fmt.Sprintf("%T", item))
}
q.Q = append(q.Q, item)
return nil
}
func main() {
myQueue := new(queue)
if err := myQueue.Add(5); err != nil {
fmt.Println("Error adding to queue:", err)
}
if err := myQueue.Add(4); err != nil {
fmt.Println("Error adding to queue:", err)
}
if err := myQueue.Add("hello"); err != nil {
fmt.Println("Error adding to queue:", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment