So let's play with Sessions and make sure that they work the way that we want them to.
And I'm going to come up with a completely arbitrary and artificial test to make sure that things work.
So here's what I'm going to do.
I'm looking in the handler's folder right now.
I have two handlers.
I have one for the home page and I have one for the about page.
So what I'm going to do is this.
I am going to grab the remote IP address of the person visiting my site and store it in the home page.
And I'm going to do that on the home a stored in the session.
I'm going to do that in the home handler and that's really quite easy to do.
So I first of all, I need the remote IP address.
Well, that's easy.
I can pull that right out of the request, so I'll call a variable remote IP is assigned the value of
our remote address.
OK, so that's built right into the standard library is part of the http http package and I can just
pull the remote address.
So that'll be an IP version four or an IP version six address and that's returned as a string.
So I have a string here right now.
I want to put that in the session and I know I have access to the session from the site where I config
and I can get to the site where I config from this M variable that's part of the receiver M app session.
Put and put, as you can see, takes three arguments.
The context a key to look up the value if I want to get it out of the session and the actual value that
I'm storing in that session, larible.
So I'll put our context.
The name will be remote IP and this can be whatever I want it to be.
But that may that may make sense.
And the value is remote IP, which I just thought, OK, so if you think about this, all I'm doing
is the first time somebody hits that home page or actually every time somebody hits that home page for
that user session, I'm storing the remote IP as a string in the session with the key to look it up
being remote IP.
All right.
Well, that doesn't seem terribly helpful.
What am I going to do with this?
Well, in the other handler, I'm going to pull that value out of the session.
So I will say remote IP is assigned and app session get and I'm going to use get string so I don't have
to typecast it.
Get the string from the context arg context and the key to look it up is remote IP.
And the thing to remember here is that this value will be an empty string if there is nothing in the
session named remote IP.
So now that I have that, I need to put it in my string.
That string map oops map and I'll call it remote IP is given the value of remote IP and now I'm going
to go to my vote page template.
So let's find that about Page and we'll put a little bit logic in here.
In a new paragraph, I'm going to put an if statement in here, so this is a and if statement as part
of the template package.
So it doesn't follow the standard go syntax, but it's really simple, if not equal to.
I'm going to make sure that things are not equal.
So if not equal to and then look up the first value index dot string map, remote IP and it has to match
case if that's not equal to nothing.
In other words, if there's a value stored in that string, that's not an empty string.
I want to do something else.
And
all right.
So if my value stored in the string map is not equal to an empty string, then I want to say your remote
IP address is and then I'll just look up that value again.
So I'll just copy and paste this copy and paste.
Otherwise I got at the wrong spot.
Paste, otherwise, I don't know your IP address yet, visit the atrip equals slash homepage.
So I can set it all right now, this should all work.
Let's give this a try.
First of all, let me go back to my Web browser, make sure I'm not on the homepage.
Slug's close that tab out.
I'll open New Initech and then close this one and close this one and close this one.
All right.
So I'm starting from a known place.
Go run command web, me or StarTalk, go.
It compiles good and now I'm going to go to localhost, but I want to go to the vote page because I
don't want to set that sectional variable yet, I hit it and it does not connect rom part number eighty
eighty.
This is the vote page.
I don't know your IP address yet.
Visit the home page so I can set it.
So I'll go to the homepage.
And now that I visited that home page, when I go back I should get a different message because it should
be able to pull a non-empty string out of this session that has my remote IP address and there it is,
one two seven zero zero one and then appart number after.
So that is exactly what I wanted to do and that's how sessions work.
So we have just so you know, let me go back to the record, not the roots, the handlers.
We have a bunch of ways of getting things out of a session or putting things or one way to put it in,
but a bunch of ways of getting it out.
So up here, when I said Aptos session, get string.
Let's look at some of the other things that are built into the session.
Package Emden after session dot I can get direct access to the cookie I can find the lifetime of the
session.
I can look for the codec which are never going to do.
But it's there if you want it.
I can get a string, I can get anything I want out of the session.
So often I'll store something that's not a string, it's not a an integer, one of the default primitive
types.
I can put whatever I want in the session.
To do that I have to make one extra step.
But we're not going to do that for a little while.
But just so you know, the session package is pretty simple to use.
And I like Alex Edwards excession.
It works extremely well if you want to experiment with another one.
Well, it's not that difficult.
You already know how to install a package.
Give it a try if you're so interested.
Anyway, that's it for Sessions for right now.
We'll be coming back to the usage of sessions on a regular basis as our application takes shape.
But I think we've covered the basics for now.