Need explaination for code

103 views
Skip to first unread message

thatipelli santhosh

unread,
Jul 26, 2019, 2:16:02 PM7/26/19
to golang-nuts
Hi ,

I am learning Go step by step. I have go through with slices and structs but this below code sample is using slices and structs both at a time.

Can anyone please explain this piece of code? this would be more helpful to me to understand this. Thank you!

var testCases = []struct {
 description
string
 planet      
Planet
 seconds     float64
 expected    float64
}{
 
{
 description
: "age on Earth",
 planet
:      "Earth",
 seconds
:     1000000000,
 expected
:    31.69,
 
},
 
{
 description
: "age on Mercury",
 planet
:      "Mercury",
 seconds
:     2134835688,
 expected
:    280.88,
 
},
 
{
 description
: "age on Venus",
 planet
:      "Venus",
 seconds
:     189839836,
 expected
:    9.78,
 
},
 
{
 description
: "age on Mars",
 planet
:      "Mars",
 seconds
:     2329871239,
 expected
:    39.25,
 
},
 
{
 description
: "age on Jupiter",
 planet
:      "Jupiter",
 seconds
:     901876382,
 expected
:    2.41,
 
},
 
{
 description
: "age on Saturn",
 planet
:      "Saturn",
 seconds
:     3000000000,
 expected
:    3.23,
 
},
 
{
 description
: "age on Uranus",
 planet
:      "Uranus",
 seconds
:     3210123456,
 expected
:    1.21,
 
},
 
{
 description
: "age on Neptune",
 planet
:      "Neptune",
 seconds
:     8210123456,
 expected
:    1.58,
 
},
}


Brian Hatfield

unread,
Jul 26, 2019, 2:33:05 PM7/26/19
to thatipelli santhosh, golang-nuts
Welcome to Go!

There's a couple things going on here, which you can break down into two overarching parts.

Part 1, var/type definition:

var testCases = []struct {
 description 
string
 planet      
Planet
 seconds     float64
 expected    float64
}

This says the following things:
  • Create a new variable, called testCases
  • testCases will contain a slice of (anonymous) structs ( []struct {.....} )
  • and each of those structs will contain 4 fields: description, planet, seconds, expected
Part 2, the instantiation/struct literal:

{
 
{
 description
: "age on Earth",
 planet
:      "Earth",
 seconds
:     1000000000,
 expected
:    31.69,
 
},
 
{
 description
: "age on Mercury",
 planet
:      "Mercury",
 seconds
:     2134835688,
 expected
:    280.88,
 
},
[....etc....]
}

This says:
  • Instantiate this slice of structs right in-line (this is called a literal)
  • Repeated anonymous struct literals for each item in the slice
The likely purpose of this code, overall, is so that later on down, something can iterate `testCases` and get a number of `testCase`, each with test expected values.

You could also write this code like:

type TestCase struct {
 description string
 planet      
Planet
 seconds     float64
 expected    float64
}

var testCases []TestCase

testCases = append(testCases, TestCase{
 description: "age on Uranus",
 planet
:      "Uranus",
 seconds
:     3210123456,
 expected
:    1.21,
})

[.....etc....]

with an append call for each case you want to add.

The example you provided just streamlines the whole process, and takes advantage of slice literals, anonymous struct definitions, and implicit struct literals within a slice definition.

Hope that helps!
Brian

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/de5ac14e-0f10-4e84-ae07-10aecd9eb11d%40googlegroups.com.

Jan Mercl

unread,
Jul 26, 2019, 2:36:43 PM7/26/19
to thatipelli santhosh, golang-nuts
On Fri, Jul 26, 2019 at 8:15 PM thatipelli santhosh
<santhoshth...@gmail.com> wrote:

> Can anyone please explain this piece of code? this would be more helpful to me to understand this. Thank you!

I don't know what "explain this code" means in this case. But maybe
this can help: https://play.golang.org/p/t4yDlOwG-rv

thatipelli santhosh

unread,
Jul 27, 2019, 2:26:01 AM7/27/19
to Brian Hatfield, golang-nuts
Thank you for explaining that @brian Hatfield
Reply all
Reply to author
Forward
0 new messages