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

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,152 questions

40,706 answers

573 users

How to use pointers with struct in Go

2 Answers

0 votes
package main
 
import "fmt"
 
type S struct {
    s1, s2 string
}
 
var (
    a = S{"go","java"} 
    b = &S{"c","c++"}
    c = S{s1:"php",s2:"python"}
    d = S{}
)
 
func main() {
    e := b
    b.s1 = "abc"
    f := *b
	
    fmt.Println("a:", a)
    fmt.Println("b:", b)
    fmt.Println("c:", c)
    fmt.Println("d:", d)
    fmt.Println("e:", e)
    fmt.Println("e:", f)   
}  

  
  
/*
run:
  
a: {go java}
b: &{abc c++}
c: {php python}
d: { }
e: &{abc c++}
e: {abc c++}
  
*/

 





answered Aug 9, 2020 by avibootz
0 votes
package main 
  
import "fmt"
  
type Worker struct { 
    name  string 
    age int
} 
  
func main() { 
    wo := Worker{"Tom", 45} 
  
    p := &wo
  
    fmt.Println(p) 
  
    fmt.Println(p.name) 
    fmt.Println((*p).name) 
} 

  
  
/*
run:
  
&{Tom 45}
Tom
Tom
  
*/

 





answered Aug 9, 2020 by avibootz

Related questions

1 answer 62 views
62 views asked Aug 25, 2020 by avibootz
1 answer 99 views
99 views asked Feb 25, 2020 by avibootz
1 answer 77 views
1 answer 68 views
68 views asked Aug 24, 2020 by avibootz
1 answer 107 views
...