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 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 147 views
147 views asked Aug 25, 2020 by avibootz
1 answer 171 views
171 views asked Feb 25, 2020 by avibootz
1 answer 182 views
1 answer 150 views
150 views asked Aug 24, 2020 by avibootz
1 answer 200 views
1 answer 155 views
155 views asked Mar 7, 2020 by avibootz
6 answers 499 views
499 views asked Feb 25, 2020 by avibootz
...