Make an empty stack slice in a struct

type Stacker interface {
isEmpty()bool
size()int
// many more function definitons
}

type StackSlice struct {
    s []int;
    x int
}

func makeStackSlice() Stacker {
    s := &StackSlice{
        s: make([]int,0, 100),
}
return s
}

Gives me error- cannot use s (type *StackSlice) as type Stacker in return argument:
*StackSlice does not implement Stacker (missing isEmpty method)

StackSlice does not implement Stacker so you can’t return it from makeStackSlice. Add implementations like this:

func (*StackSlice) isEmpty() bool {
	return false
}

func (*StackSlice) size() int {
	return 0
}

Then your code works: https://goplay.space/#4VQdC2fvv5V

Hey

Thank You for your help. Using makeStackSlice() is a requirement for my
assignment. Let me know if you get that part…Instruction as follows

Create a new struct called StackSlice that implements Stacker, and uses an
int slice as its internal representation.

In addition to implementing all of the methods in Stacker, write a
makeStackSlice() function (not method!) with this header:

// returns a new empty stack using an int slice representationfunc
makeStackSlice() Stacker {
// …}

You can use it like this:

s := makeStackSlice()s.push(4)// …

So this is homework. I suggest you read a good Go book and complete the Go Tour. You should easily be able to finish this assignment.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.