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 nested struct as array in Go

1 Answer

0 votes
package main
 
import "fmt"
 
type A struct {
    result float64
}
 
type B struct {
    s string
    n int
    arr []A
}
 
func main() {
    st := B {s: "go",
             n: 18943,
          arr: []A {A {result: 3.14},
                    A {result: 98.432},
                    A {result: 981.12},
                   },
    }
    fmt.Println(st.s)
    fmt.Println(st.n)
    
    fmt.Println(st.arr[0])
    fmt.Println(st.arr[1])
    fmt.Println(st.arr[2])
    
    fmt.Println(st.arr[0].result)
}
 
 
 
/*
run:
 
go
18943
{3.14}
{98.432}
{981.12}
3.14

*/

 



answered Aug 24, 2020 by avibootz
edited Aug 24, 2020 by avibootz

Related questions

1 answer 200 views
3 answers 243 views
2 answers 231 views
231 views asked Sep 4, 2020 by avibootz
1 answer 181 views
2 answers 204 views
204 views asked Aug 9, 2020 by avibootz
1 answer 154 views
154 views asked Mar 7, 2020 by avibootz
6 answers 499 views
499 views asked Feb 25, 2020 by avibootz
...