1
00:00:01,470 --> 00:00:07,570
So another thing that you use go for quite regularly is to read and write from Jason.

2
00:00:07,650 --> 00:00:10,030
And if you're not familiar with Jason, don't worry about it.

3
00:00:10,050 --> 00:00:11,790
There's an example right here in front of you.

4
00:00:12,390 --> 00:00:16,500
Jason stands for Jason, as some people pronounce it.

5
00:00:16,910 --> 00:00:19,950
It stands for JavaScript object notation.

6
00:00:19,950 --> 00:00:27,060
And it's just a way of getting information from one place to another in a structured format and go makes

7
00:00:27,060 --> 00:00:29,530
it really, really easy to work with Jason.

8
00:00:29,700 --> 00:00:37,050
So typically, you would call some external service an API on some external service and you would receive

9
00:00:37,050 --> 00:00:41,180
from your call a response that consists of a file like this, Jason.

10
00:00:41,670 --> 00:00:47,940
So it's a structured format and this one actually starts and ends with square brackets.

11
00:00:47,950 --> 00:00:54,600
So this is what you might think of as an array or a number one or more entries that all have a similar

12
00:00:54,600 --> 00:00:55,020
format.

13
00:00:55,590 --> 00:00:57,450
So in this case, we have two people.

14
00:00:57,570 --> 00:00:59,280
We have one that's named Clark Kent.

15
00:00:59,820 --> 00:01:00,870
His first name is Clark.

16
00:01:00,870 --> 00:01:02,000
His last name is Kent.

17
00:01:02,010 --> 00:01:04,020
He has black hair and he has a dog.

18
00:01:04,620 --> 00:01:06,690
And the next one is Bruce Wayne.

19
00:01:06,930 --> 00:01:09,630
And he has black hair, but he doesn't have a dog.

20
00:01:09,630 --> 00:01:14,490
And I don't know if Bruce Wayne actually had a dog or not, but for the purposes of this exercise,

21
00:01:14,490 --> 00:01:15,150
he does not.

22
00:01:15,840 --> 00:01:21,180
So imagine, if you will, that you've called some external external service and you've received this

23
00:01:21,180 --> 00:01:27,930
response and you need to get it into a format that you can use because this is just a string and this

24
00:01:27,930 --> 00:01:29,990
is not something we can work with very easily.

25
00:01:30,000 --> 00:01:33,630
So one of the things we can do is actually define a type.

26
00:01:34,080 --> 00:01:37,170
So I'm going to define a type called person.

27
00:01:37,950 --> 00:01:44,100
And this is just my own data structure and it is a struct and it has a few members and the members in

28
00:01:44,130 --> 00:01:49,400
this type correspond to the entries in each one of these things in the adjacent file.

29
00:01:49,740 --> 00:01:55,050
So I'll call my first one first name and it's a string and then I'm going to put this special tag after

30
00:01:55,050 --> 00:01:55,200
it.

31
00:01:55,200 --> 00:02:02,700
And this will tell go that when you receive or you're creating Jasen in this format, here's what you

32
00:02:02,700 --> 00:02:12,090
use for the the name's Jason Colon, that in quotes first underscore name and this exactly matches what

33
00:02:12,090 --> 00:02:12,500
is here.

34
00:02:12,930 --> 00:02:14,670
So I'll do the same thing for last name.

35
00:02:17,620 --> 00:02:24,880
And again, the entry in Jason would be last underscore name and hair color.

36
00:02:27,760 --> 00:02:37,660
Jason Hare underscore Cullen and finally has dug, but this is not a string, this is a bull, Julian,

37
00:02:38,470 --> 00:02:41,710
and this is going to be has underscored dog.

38
00:02:42,130 --> 00:02:47,390
So now that I've defined this type, I can use that type to unmerciful.

39
00:02:47,410 --> 00:02:53,650
Jason, this Jason file, which we're pretending we got from an external API into a struct.

40
00:02:53,680 --> 00:03:01,240
So what we do, first of all, is we declare a variable and I'll call my variable unmuzzled and I'm

41
00:03:01,240 --> 00:03:08,850
calling it that because the functions used in the Jason package in Gollan are called Mercial and unmerciful.

42
00:03:08,890 --> 00:03:13,510
So this one is unmuzzled and it's going to be a type of slice person.

43
00:03:14,170 --> 00:03:20,740
And the reason it's a slice is because this Jason, in the format that it's presented here has one or

44
00:03:20,740 --> 00:03:21,550
more entries.

45
00:03:21,580 --> 00:03:22,180
Here's one.

46
00:03:22,180 --> 00:03:22,810
Here's two.

47
00:03:22,810 --> 00:03:23,800
It might only have one.

48
00:03:23,800 --> 00:03:24,790
It might have a thousand.

49
00:03:25,060 --> 00:03:31,390
But I'm creating a slice because I know that what I'm getting from my Jason might have more than one

50
00:03:31,390 --> 00:03:31,780
entry.

51
00:03:32,050 --> 00:03:37,780
So how do I get it out of Jason into my variable that I've just created called on Marshall?

52
00:03:37,900 --> 00:03:38,560
Well, it's quite easy.

53
00:03:39,100 --> 00:03:45,490
Error is assigned the value of Jason, and that's the package in the standard library.

54
00:03:45,820 --> 00:03:49,210
And I'm going to call the function on Marshall now.

55
00:03:49,210 --> 00:03:52,780
And Marshall, when we point out it, you see that it takes two parameters.

56
00:03:52,810 --> 00:03:55,160
One is a slice of bytes.

57
00:03:55,250 --> 00:03:58,150
Well, this is a string, so we need to convert that to a slice of bytes.

58
00:03:58,150 --> 00:03:59,330
But that easy, that's easy.

59
00:03:59,830 --> 00:04:06,070
And the second is an interface that you're going to put the contents of this slice of bytes into, and

60
00:04:06,070 --> 00:04:07,650
that will be our variable on martialled.

61
00:04:08,110 --> 00:04:10,900
So how do I convert from a string to a slice of bytes?

62
00:04:10,930 --> 00:04:11,610
Well, it's quite easy.

63
00:04:12,370 --> 00:04:13,600
Slice byte.

64
00:04:13,600 --> 00:04:17,440
And then in parentheses, the data that I want to convert to a slice of bytes.

65
00:04:17,440 --> 00:04:20,350
And in this case it's the variable called my JSON.

66
00:04:21,250 --> 00:04:22,780
And where do I want to put this?

67
00:04:22,870 --> 00:04:26,230
I want to put it into a reference to a commercial.

68
00:04:27,610 --> 00:04:28,810
Now we check for our error.

69
00:04:28,840 --> 00:04:40,030
If error is not equal to nil, then I shall just say log print line error, martialing Jason and then

70
00:04:40,030 --> 00:04:40,710
I'll print the error.

71
00:04:42,580 --> 00:04:43,010
All right.

72
00:04:43,030 --> 00:04:45,320
So once I have this, what can I do with it?

73
00:04:45,340 --> 00:04:46,390
Well, let's print it out.

74
00:04:46,390 --> 00:04:53,440
Log print F and I'm going to use the print F because I want to substitute something for a placeholder

75
00:04:53,440 --> 00:04:54,730
and I'll just put on martialled.

76
00:04:56,490 --> 00:05:03,840
Oops, I got to put that in quotation marks on martialled colon and then present the subject, putting

77
00:05:03,840 --> 00:05:07,920
an interface in there and what I want to put in there is my variable on martialed.

78
00:05:08,550 --> 00:05:08,910
All right.

79
00:05:08,940 --> 00:05:10,280
So let's see if we got this right.

80
00:05:10,290 --> 00:05:14,190
Let's run this go run may not go.

81
00:05:15,990 --> 00:05:18,270
And we have this value given back.

82
00:05:18,270 --> 00:05:20,310
And Marshall is Clark Kent Black.

83
00:05:20,310 --> 00:05:20,730
True.

84
00:05:20,910 --> 00:05:26,760
Bruce Wayne Black Fault's, which exactly corresponds to the entry in Jason in the Jason file.

85
00:05:27,090 --> 00:05:29,070
But now it's been converted to obstruct.

86
00:05:29,190 --> 00:05:30,110
So that's pretty easy.

87
00:05:30,540 --> 00:05:33,750
So we can read Jason into a struct.

88
00:05:34,020 --> 00:05:36,480
How do we write Jason from a struct.

89
00:05:36,540 --> 00:05:40,410
Well that's pretty easy to write, Jason.

90
00:05:40,410 --> 00:05:41,670
From a struct.

91
00:05:41,700 --> 00:05:43,050
Well, first of all, let's create a struct.

92
00:05:44,220 --> 00:05:48,680
So I will call the struct my slice and it's going to be a slice.

93
00:05:48,720 --> 00:05:53,500
Sorry, Daria, that again ver my slice is going to be a slice of person.

94
00:05:54,570 --> 00:05:56,040
And now let's put some values in there.

95
00:05:56,040 --> 00:06:05,460
So I'll say ver mercial, I'll call it meone just for Mercial me one and it's going to be a person and

96
00:06:05,460 --> 00:06:06,480
let's put some values in there.

97
00:06:06,480 --> 00:06:16,590
And one dot first name is equal to I don't know, we need another superhero Wally and one dot last name

98
00:06:17,910 --> 00:06:18,960
equals W.

99
00:06:21,610 --> 00:06:31,540
And one good hair color equals red and one has Doug equals false, I'm not sure if the Flash has a dog

100
00:06:31,540 --> 00:06:37,040
or not, but again, this is just the purpose of this exercise is just to show how things work.

101
00:06:37,060 --> 00:06:41,320
So my superhero knowledge may be somewhat limited and I apologize for that.

102
00:06:41,830 --> 00:06:45,160
Now I'm going to append this and one variable to this slice.

103
00:06:45,160 --> 00:06:47,640
So I'll put an entry into this slice and that's pretty easy.

104
00:06:47,980 --> 00:06:54,470
My slice equals append and I'm appending to my slice and I'm putting in the value add one.

105
00:06:55,120 --> 00:06:59,560
Now let's copy this whole thing and just change the variable names and the values.

106
00:06:59,560 --> 00:07:05,080
So we'll have to represent our slice of our M2 and I'll change these all to M2.

107
00:07:10,920 --> 00:07:13,050
And I'll change this to Diana.

108
00:07:14,850 --> 00:07:15,420
Prince.

109
00:07:17,730 --> 00:07:23,580
Black and she does not have a dog either, and now I'll put em to into this slice.

110
00:07:24,360 --> 00:07:30,090
So now I've created a slice that consists of two entries it has and one which is not called in, one

111
00:07:30,090 --> 00:07:33,750
in the slice, but it has an entry with Wally West and an entry with Diana Prince.

112
00:07:34,800 --> 00:07:41,850
So now I want to convert this into Jason and I'll create a variable called Jason and potentially an

113
00:07:41,850 --> 00:07:42,270
error.

114
00:07:42,720 --> 00:07:46,560
And that is a sign the value of and we call the Jason package again.

115
00:07:46,740 --> 00:07:48,480
And this time we want to Marshall.

116
00:07:48,510 --> 00:07:52,620
Now I'm going to use Marshall indent just because it makes it more readable.

117
00:07:52,860 --> 00:07:55,380
All that does is format our Jason nicely.

118
00:07:55,620 --> 00:07:59,730
If I just use Marshall, it wouldn't have any nice tabs or line breaks.

119
00:07:59,730 --> 00:08:02,500
It would make it really as small as it possibly could.

120
00:08:02,730 --> 00:08:09,450
So in production, when you're sending Jason, say, to a remote request, say you build an API that

121
00:08:09,960 --> 00:08:13,260
responds with Jason, you probably wouldn't use Marshall in debt.

122
00:08:13,260 --> 00:08:18,210
But for development purposes, when I want to look at the content that I'm producing, I always use

123
00:08:18,210 --> 00:08:18,960
Marshall in debt.

124
00:08:19,200 --> 00:08:21,950
So what are we martialing into Jason?

125
00:08:22,110 --> 00:08:26,370
We're marshaling my slice and it has no prefix.

126
00:08:26,370 --> 00:08:30,720
You can put a prefix in there if you want to, but you rarely do, or at least I rarely do.

127
00:08:30,720 --> 00:08:32,430
You might find situations where you have to.

128
00:08:32,520 --> 00:08:33,480
And how much do you want to win?

129
00:08:33,660 --> 00:08:35,220
It's identity, a few spaces.

130
00:08:36,150 --> 00:08:39,330
So we've called that we should check for our error.

131
00:08:39,360 --> 00:08:46,170
If error is not equal to nil, then I will log print line error martialing.

132
00:08:51,790 --> 00:08:56,210
And I'll just put the air in there, otherwise I want to print this out.

133
00:08:56,410 --> 00:08:58,740
Now, what does Jason Marshall intent give us?

134
00:08:59,200 --> 00:09:04,750
It hands back a slice of bytes and potentially an error so I could just write the slice of bytes, but

135
00:09:04,750 --> 00:09:05,980
that would not be very useful.

136
00:09:05,980 --> 00:09:07,880
You wouldn't get much useful information from that.

137
00:09:08,170 --> 00:09:17,560
So instead, let's just use our format, package format or print F or format print, and we'll convert

138
00:09:17,560 --> 00:09:23,200
our slice of bytes into a string by saying String and new Jason.

139
00:09:24,250 --> 00:09:26,360
All right, let's try this out and see how it works.

140
00:09:26,360 --> 00:09:27,430
So I'll clear the screen.

141
00:09:31,070 --> 00:09:33,590
And I'll make this a little bit bigger so I can see what's going on.

142
00:09:35,850 --> 00:09:39,720
Let's run this go, run, mean, go.

143
00:09:42,610 --> 00:09:49,510
So first of all, I get the content from the first part of our program where it actually creates a struct

144
00:09:49,510 --> 00:09:50,860
from Jason, there it is.

145
00:09:51,160 --> 00:09:55,930
And then it outputs my JSON that I constructed as a slice of struct.

146
00:09:56,470 --> 00:09:58,990
It outputs it as Jason, and that's all there is to it.

147
00:09:59,290 --> 00:10:05,200
Now, this presupposes that, you know, the format of the JSON that you're receiving, if you're receiving

148
00:10:05,200 --> 00:10:08,650
JSON, if you don't know the format, there's a way to handle that.

149
00:10:08,650 --> 00:10:10,390
But I'm not going to cover that in this lecture.

150
00:10:10,390 --> 00:10:13,570
If we run into that situation later in the course, we will cover it.

151
00:10:14,860 --> 00:10:19,660
But in the most cases, it's it's always best if you know the structure of the duration you're going

152
00:10:19,660 --> 00:10:22,270
to be receiving, just write a struct.

153
00:10:22,270 --> 00:10:26,140
The conforms to that structure and it will make your life ever so much easier.

154
00:10:26,530 --> 00:10:26,860
All right.

155
00:10:26,860 --> 00:10:28,280
That's enough for this time around.

156
00:10:28,300 --> 00:10:28,990
Let's move on.
