Created
May 12, 2018 06:26
-
-
Save ENvironmentSet/a6de03475a20f08861657e7911401e75 to your computer and use it in GitHub Desktop.
Simple Stack based on Go slice
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 "fmt" | |
type any interface {} | |
type Stack []any | |
func (stack *Stack) Push(elements ...any) Stack { | |
for _, element:= range elements { | |
*stack = append(*stack, element) | |
} | |
return *stack | |
} | |
func (stack *Stack) Pop() (result any) { | |
result = (*stack)[len(*stack) - 1] | |
(*stack)[len(*stack) - 1] = nil | |
*stack = (*stack)[:len(*stack) - 1] | |
return | |
} | |
func main() { | |
var stack Stack = Stack{0, 1, 2, 3, 4, 5} | |
stack = stack.Push(6, 7, 8) | |
fmt.Print(stack.Pop()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment