Create a slice of string

Create a slice of a slice of string ([][]string). Store the following data in the multi-dimensional slice:

            "James", "Bond", "Shaken, not stirred"

	"Miss", "Moneypenny", "Helloooooo, James."

Range over the records, then range over the data in each record.

:star: This was the instructions for a hands-on exercise.

This is my solution, after some help from people here.

https://play.golang.org/p/FkXq0dfL1jb

This is the teacher’s solution.

https://play.golang.org/p/A0-wVdlliHx

Please explain the difference.

2 Likes

On a quick glance through my mobile, I can see difference only in lack of formatting on your solution and order of statements.

But maybe I’ve missed some things, as I’m only on mobile right now.

I don’t see any differences in this code. This is from Tood McLeod course, right? :yum:
One difference is in inside of for loop. You are printing record id and values

fmt.Println("record", i, v)

and teacher printing only record id

fmt.Println("record: ", i)

Very good. You’re making great progress. Use the format button to get the formatting, for example the curly brackets in the correct places.

Hi Cherolyn,

You did very well with that! It looks like there is just one little bug.

In addition to what Johan pointed out, there is this:

for j, val := range rt {

does something different than

for j, val := range xs {

Notice that it is xs here, not xxs. So the results are different. Your version prints only two of the three strings in each slice. Change rt to v in your code, and it will print all three.

1 Like

Thanks. I see you are right

What’s a format button?

And thanks for the compliment

You’re right. Todd McLeod.
You’re also right about the second statement. I don’t understand the difference between
fmt.Println(“record”, i, v) and fmt.Println("record: ", i)

Interesting. What does “v” that you suggested, what does it signify?

Also, the teacher used xs1,xs2, and xxs. Do you see a reason he might have done that?

Is there a difference between val and v?

It’s the button next to Share button in go playground. It changes how the program is formatted so it follows the usual way of formatting go programs. How many spaces here and there and so on

Hi Cherolyn,

Here is your program at the Go Playground, with the change I suggested:

Look at the second for loop. I changed range rt to range v to make it work more like the teacher’s solution. Try running it. Now it will print all three strings in each of the two slices.

1 Like

They are just variable names, so you can use anything you want. The ones he chose weren’t very descriptive! It’s best to use variable names that help show what they are used for, to make your code easy to understand.

1 Like

You’re sharp. What’s id?

I tried that! the format button. Really cool! I had to study the difference very carefully

You are right! What made you think of v instead of rt?

Interesting. Do you have any suggestions for this case?

Studying Go in the morning wakes me up more effectively than a cup of coffee, which I love!

1 Like