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,959 questions

51,901 answers

573 users

How to use struct in Go

6 Answers

0 votes
package main

import "fmt"

type S struct {
	id, age int
	name string
}

func main() {
	fmt.Println(S{2384, 47, "tom"})
	
	fmt.Println(S{age: 30})
}


/*
run:

{2384 47 tom}
{0 30 }

*/

 



answered Feb 25, 2020 by avibootz
0 votes
package main

import "fmt"

type S struct {
	id, age int
	name string
}

func NewWorker1(name string) *S {
	w := S{name: name}
    w.age = 51
	w.id = 3112
    return &w
}

func NewWorker2(name string) S {
	w := S{name: name}
    w.age = 32
	w.id = 2983
    return w
}


func main() {
	fmt.Println(NewWorker1("Dan"))
	fmt.Println(NewWorker2("Ava"))
}


/*
run:

&{3112 51 Dan}
{2983 32 Ava}

*/

 



answered Feb 25, 2020 by avibootz
0 votes
package main

import "fmt"

type S struct {
	id, age int
	name string
}

func main() {
	w := S{id: 5631, age: 55, name: "Arthur"}
    
	fmt.Println(w.id)
	fmt.Println(w.age)
	fmt.Println(w.name)
}



/*
run:

5631
55
Arthur

*/

 



answered Feb 25, 2020 by avibootz
0 votes
package main

import "fmt"

type S struct {
	id, age int
	name string
}

func main() {
	w := S{id: 5631, age: 55, name: "Arthur"}
	
	p := &w
    fmt.Println(p.age)
	
	p.age = 80
    
	fmt.Println(w.id)
	fmt.Println(w.age)
	fmt.Println(w.name)
}



/*
run:

55
5631
80
Arthur

*/

 



answered Feb 25, 2020 by avibootz
0 votes
package main

import "fmt"

type S struct {
	id, age int
	name string
}

func main() {
	w := S{3421, 29, "Isla"}
	
    fmt.Println(w)
}



/*
run:

{3421 29 Isla}

*/

 



answered Feb 25, 2020 by avibootz
0 votes
package main

import "fmt"

type S struct {
	id, age int
	name string
}

func main() {

	p := new(S)

	p.id = 3982
	p.age = 32
	p.name = "tom"
	
    fmt.Println(p)
}



/*
run:

&{3982 32 tom}

*/

 



answered Feb 25, 2020 by avibootz

Related questions

1 answer 183 views
1 answer 152 views
152 views asked Aug 24, 2020 by avibootz
1 answer 200 views
2 answers 207 views
207 views asked Aug 9, 2020 by avibootz
1 answer 158 views
158 views asked Mar 7, 2020 by avibootz
1 answer 163 views
3 answers 245 views
...