Hello and welcome to the Golang programming language course.
In the previous session, we talked about J Song concept, and in this session we want to talk about
the others Jason concepts.
Converting a gold tie into Jason in gold, we can convert data to Jason to do this.
We use machine function.
The marshal function in package, including Jason, is used to encode the data into JSON format, martial
function exits an empty interface and returns an array of by an error message.
Now you can see the syntax.
Let's go to this school, Cary Grant, to illustrate how to convert Golang data into JSON format by
using machine function.
First, go to Explorer and create a main fine
set package me and define a main function.
First, create a struct like person, including some fields, so the right type person is struct and
defined.
Some fields like name by the type, string and age by the time in 64 and location by the time estrange.
Now go to main function to convert go blank object to JSON format.
First, create an object from person a struct and initialize it.
So define a variable like person column equals person struct and initialize the value.
Full name, Gene, for age 20.
And for the location, Texas,
now we use Marshall function to quote object into JSON format.
So first, create two variables for returns values that bind, array and error, so define per cent
array and error column equals and calling marschall function from JSON packet.
Jason Stuart Marsh and third person object and argument to it.
Take our status by the following quote
no matter the
in to and.
Now, Chast returns value to shrink and display it off the chest with a screen and served first time,
a reform of the code and save the project and execute the program,
go from the splash, made that go.
Now we can see the output name is king, age is 20 and the location is takes us, we are encoding person
distraught in JSON format.
First, create a new object of person, then encode the person in JSON format using JSON that on function.
If everything goes well, then the error will be Neil and person error is the representation of a person
in byte at a forum.
Now take a look at the person.
This struct, the location field, is thought by the author chase.
Now you can see it, so you want to change it to larges and run program again.
Save the project and execute again.
OK.
We can see my name is Gene and ages 20.
The location field could not map to Jason, and we don't see location fall in output.
So if you want to change a field name for map to the astronaut field, we use the Jason tank, for example.
You have a strong field as name, but you want to map it as a first name in JSON format.
To do this, you can use the JASON tag to the struct field notes that don't give any space between defined
JSON tag name as it between zero.
Now we will apply this change to our program and execute again.
Go to the person extract.
So be right back.
Take Jason.
Double quotation mark and set first name first name.
Reformat the code, save the project and execute again.
OK.
First name is Gene, age is 20 and location is taking us.
The program ran without any problems, and we could change variable name to first name by using the
Jason tank.
Go to the slides and continue.
Ahmed Empty, we have a special Jason tag as Ahmed Empty if a field is set as Ahmed A..
Then it will not end quote the JSON format if it is empty, so go to a school program to illustrate
Ahmed A..
For example, set location field as Ahmed A. and execute program again.
So be right back.
Take Jason Colleen double quotation mark since location by lowercase and sit on it and to.
Now go to main function and create an object from person is the without initialize location field.
So.
Define a variable like person, call an equals person for the name, be, said Jessica, and for the
age, we said 30.
And not set any value for location reform at the called Save the Project and execute the program again.
Now we can see the output first name is Jessica and ages 30, and the program ran without any problems.
In order not to prolong the time of decision, we will finish it and we will continue the content in
the next session until next session.
Goodbye.