Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to pass struct to a function as pointer in Go

3 Answers

0 votes
package main
 
import (
    "fmt"
)
 
type S struct {
    x, y float64
}
 
func (st *S) Add(f float64) {
    st.x = st.x + f
    st.y = st.y + f
}
 
func main() {
    st := S{2, 7}
    st.Add(10)
    fmt.Println(st.x, st.y)
}
 
 
 
/*
run:
 
12 17
 
*/

 



answered Mar 17, 2020 by avibootz
edited Mar 17, 2020 by avibootz
0 votes
package main

import (
	"fmt"
)

type S struct {
	x, y float64
}

func f(st *S, f float64) {
	st.x = st.x + f
	st.y = st.y +  f
}

func main() {
	st := S{3, 8}
	f(&st, 10)
	fmt.Println(st.x, st.y)
}



/*
run:

13 18

*/

 



answered Mar 17, 2020 by avibootz
0 votes
package main
  
import (
    "fmt"
)
  
type S struct {
    x, y float64
}
  
func (st *S) Add(f float64) {
    st.x = st.x + f
    st.y = st.y + f
}
  
func main() {
    st := &S{4, 6}
    st.Add(10)
    fmt.Println(st.x, st.y)
}
  
  
  
/*
run:
  
14 16
  
*/

 



answered Mar 17, 2020 by avibootz

Related questions

1 answer 188 views
188 views asked Nov 4, 2020 by avibootz
1 answer 170 views
1 answer 182 views
1 answer 155 views
155 views asked Mar 7, 2020 by avibootz
1 answer 150 views
150 views asked Aug 24, 2020 by avibootz
1 answer 142 views
...