Go type definition and initialize a pointer

32 views Asked by At

I'm new to Go. I'm trying to use type definition to customize a stack.

type Stack []int

func main() {
    slice := make([]int, 0)
    var stack Stack = make([]int, 0)
    fmt.Println(slice, stack)
    // var pstack *Stack = &slice

Type Stack is an []int.

I found it possible to assign the result of make([]int, 0) to the variable stack of type Stack. However, when I try to initialize a pointer of stack using & in front of the variable slice, just like the commented line in the code, it raises compile error that states:

cannot use &slice (value of type *[]int) as *Stack value in variable declaration, compiler(IncompatibleAssign)

I would like to know if there is a simple way to solve this problem.

0

There are 0 answers