Created
August 2, 2020 23:15
-
-
Save mortdeus/dc6bf15c8c2858cdc7780dd7f33c26fd to your computer and use it in GitHub Desktop.
golang generic experiment
This file contains hidden or 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
arst |
This file contains hidden or 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 ( | |
"fmt" | |
) | |
export{ | |
"T": type{int, uint, struct{type, type, int}}, | |
"syncable": type{$T, bool, sync.Mutex} | |
} | |
type syncer interface{ | |
sync($T) $syncable | |
} | |
type queue($T) []$T | |
func (q *queue($T)) enqueue(v $T) { | |
*q = append(*q, v) | |
} | |
func (q *queue($syncable)) dequeue() ($syncable, bool) { | |
if len(*q) == 0 { | |
var zero $syncable | |
return zero, false | |
} | |
r := (*q)[0] | |
*q = (*q)[1:] | |
return r, true | |
} | |
func main() { | |
q := new(queue(int)) | |
q.enqueue(5) | |
q.enqueue(6) | |
fmt.Println(q) | |
fmt.Println(q.dequeue()) | |
fmt.Println(q.dequeue()) | |
fmt.Println(q.dequeue()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment