Home > OS >  Go Testing define custom structure
Go Testing define custom structure

Time:04-24

I've followed the structure in a tf file, can you help me to create a proper structure, as I'm new to Go.

Here is tf

   ipv4    = { 
      cidrblock = "10.0.0.0/16"
      secondary = [
            {
               cidrs     = "20.0.0.0/16"
               enabled   = true
            },
            {
               cidrs     = "30.0.0.0/16"
               enabled   = true
            }
      ]               
   

}

So I've an object of strings, as well a list of objects in the main object. I could make a primitive type, for example:

type ipv4 struct {
   cidrblock string
   cidrs     string
   enabled   bool
}

type ipv6 struct {
   border    string
   generate  bool
}

type Sets struct {
   Name      string
   IPv4      *ipv4
   IPv6      *ipv6
   Tags      map[string]string
   Tenancy   string
}

But I would really like to have a complex structure

CodePudding user response:

you can do something like this:

type ipv4 struct {
    cidrblock string
    secondary []ipv4secondary
}

type ipv4secondary struct {
    cidrblock string
    enabled   bool
}

and use it as this:

example := ipv4{
    cidrblock: "10.0.0.0/16",
    secondary: []ipv4secondary{
        ipv4secondary{cidrblock: "20.0.0.0/16", enabled: true},
        ipv4secondary{cidrblock: "30.0.0.0/16", enabled: true},
    },
}

here is the example: https://go.dev/play/p/U7o0BbAis9T

  • Related