Created
April 23, 2018 01:20
-
-
Save valkheim/a2f260c8e56953102702fee5dedf5bec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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