Skip to content

Instantly share code, notes, and snippets.

@foolishway
Created April 10, 2020 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foolishway/12969bed51215a225cad8c9ba57df929 to your computer and use it in GitHub Desktop.
Save foolishway/12969bed51215a225cad8c9ba57df929 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strconv"
)
type Istring interface {
string(string) string
}
type stack struct {
sItem []Istring
}
func (s *stack) push(item Istring) {
(*s).sItem = append((*s).sItem, item)
}
func (s *stack) pop() Istring{
sItems := (*s).sItem
item := sItems[len(sItems) - 1]
(*s).sItem = sItems[:len(sItems) - 1]
return item
}
type String struct{
content string
}
func (s *String) string(str string) string {
return str
}
func main() {
var stk *stack = &stack{sItem: make([]Istring, 0)}
var str *String
for i := 0; i < 10; i++ {
func(i int) {
//fmt.Println(i)
str = &String{content: strconv.Itoa(i)}
stk.push(str)
}(i)
}
var s Istring
for i := 0; i < 10; i++ {
s = stk.pop()
fmt.Println(s.string(s.(*String).content))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment