golang为何会输出了一个地址码,求救

#小白一枚,问题描述:定义了一个方法去输出,结果不是理想的输出

图片说明

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var s str
    s.Push(4)
    s.Push(4)
    s.Push(4)
    s.Push(4)
    s.Push(4)
    s.Push(4)
    s.Push(4)
    s.Push(4)
    s.Push(4)
    s.Push(4)
    s.Pop()
    s.Push(1)
    fmt.Println(s.Str)
}

type str struct {
    i int
    d [10]int
}

func (s *str) Push(k int) {
    s.d[s.i] = k
    s.i++
}

func (s *str) Pop() int {
    s.i--
    return s.d[s.i]
}

func (s str) Str() string {
    var result string
    for i := 0; i <= s.i; i++ {
        result = result + "[" + strconv.Itoa(s.i) + ":" + strconv.Itoa(s.d[i]) + "]"
    }
    return result
}

两个问题:
1. 你在main函数中调用了Str函数,因为后面没有加括号,所以并不会执行,s.Str指向一个内存地址:

// 方法一: 将s.Str赋值给一个变量,然后执行该变量,就相当于执行了s.Str()
    S := s.Str
    fmt.Println(S())
    // 方法二: 给s.Str后面加上括号
    fmt.Println(s.Str())
  1. Str()方法的for循环写错了,切片是从0索引开始的,所以for循环的范围应该是从0~i-1:
    for i := 0; i < s.i; i++ {
        result = result + "[" + strconv.Itoa(s.i) + ":" + strconv.Itoa(s.d[i]) + "]"
    }