Question about interface and structure

Hello, I’m a student majoring CE who is studying Golang.
I have a question about my code.
the function foo()'s results keep changing each calling.
I can’t understand why the address of STRUCT str’s s keeps changing.
is there anyone about this situation?

import "fmt"

type str struct {
	s int
	t int
	r int

type str2 struct {
	s string
	t string
	r string

type inter_face interface {

func (s str) foo() {

func (s str2) foo() {
	fmt.Printf("%v\t%T\t\t\n", &s.r, s)

func main() {
	var i inter_face
	a := str{
		s: 100,
		t: 1000,
		r: 10000,
	b := str2{
		s: "string",
		t: "string type",
		r: "string bool",
	i = a
	i = b

The two methods you wrote pass their receiver argument (s) by value. Therefore it is copied and gets a new address on each call. Usually methods call their receiver by reference. Try it:
func (s *str)foo()…


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