Created
April 10, 2020 06:08
-
-
Save foolishway/12969bed51215a225cad8c9ba57df929 to your computer and use it in GitHub Desktop.
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" | |
"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