Environment variables

Todd McLeod
A free video tutorial from Todd McLeod
Tenured College Professor
4.5 instructor rating • 19 courses • 309,189 students

Learn more from the full course

Learn How To Code: Google's Go (golang) Programming Language

The Ultimate Comprehensive Course - Perfect for Both Beginners and Experienced Developers

46:34:12 of on-demand video • Updated August 2019

  • The ultimate comprehensive course
  • For beginners and experienced devs
  • Taught by a university professor
  • From beginning to advanced concepts
  • Concurrency, channels, benchmarking
  • Testing, error handling, documentation
  • Hands-on exercises with solutions
  • Access to valuable code base
  • This course is tried, tested, and proven
  • Over 2.65 Million students taught
  • Lifetime course access
  • Learn at your own pace
  • 100% satisfaction guaranteed
English [Auto] So now we're going to learn about environment variables and environment variables are variables for your environment and your environment in the context of computers as your computer. Like hey this is the environment in which your computer is operating. And so that includes like you know which user is logged in. So you can have environment variables on a per user basis because that environment might be user Todd's logged in or user or Bob is now logged in. So environment variables are just like variables that we could set in the computer that the computer can then use to figure stuff out and we can see the environment variables by like right clicking on our computer in Windows so just I mean Windows Explorer here and I'm right clicking on this PC and going to properties and then inside properties I can choose advanced system settings right here and then go to environment variables right there. And so now I have some environment variables and I can expand this and make it a little bit bigger. And I have environment variables for recording PC and recording PC as the user and then system variables which are for everybody regardless of who's logged in. So there's a path environment variable up here. There's also a PATH environment variable down here. These are the environment variables to set just for the user recording PC that's the name of this user that's logged in me. In this case and this would be PATH environment variables that we could set for any user that's logged in. And likewise there's a way to set these on a Mac and Linux and I'll show you how to show you give you some direction guidance and how to do that when we're done talking about Windows. But what we want to do is we want to set some environment variables. And one of the environment variables you can see we have a route right here and go route is set to see call and backslash go. And that's the exact same thing we saw when at our terminal. And let me just clear this out. I did go see envy. Our route is set to see call and backslash go in here see call and backslash go. And so this is where that got set and go see ENVI shows us that environment variables for the Go programming language. These are the environment variables that go programming language will use go is going to want to know where is the binary for everything I need to run go. And the route is right there right. It's going to want to know where is your workspace. Well we're going to tell it where the workspace is we need to set the go path. And where does that go path being set. So one place we can look is right here in the environment variables of this path variable. So I kind of think about environment variables as places I'm told in the computer to go look to do stuff like if I was running a game and you know I said OK launch this game. My computer is going to start looking down those environment variables to find where is the code to run that game. And I say launch go my computer will start looking down those environment variables and they'll finally find go be like oh here's go ok cool I'm going to launch it. Right. I kind of think of it like you know I tell my computer you know compile this program or run this program with GO RUN THIS GO command and my computer will go down these environment variables and kind of like knock in the store do you know how to run go code and they'll be like No I only know how to do complex stuff here and then I'll come to this when do you know how to run go code. No. I do. Good stuff. OK. Thank you and then will come to this one. Do you know how to run go code. Why yes I do. I'm glad you're here where's the code. I'll run it for you. That's kind of how I think of environment variables. So we're going to edit this environment variable. And I don't see go path in there. So I'm just going to come up here and check this one and edit it and and maybe in here Python python app data cancell And then here let's just check this ones once more. And we have Sego been SIGO been program system root. None of those. I want to just see as go pat down here. Variable. So I don't know where this go path is actually being set. All right. So I have no idea where that go path is being set. And this is a good point to talk about two things. One is that to be a programmer is to always operate on the edge of not knowing there's only so much you know nobody knows everything and eventually you have to solve problems like I don't know that yet. Let me see if I can figure it out. So to be a programmer is to always operate on the edge of not knowing and then to the other important thing is to learn which to pick your battles to learn which battles to fight and when to switch to let go. So I've no idea where that go path is being set. I normally code on Unix on a Mac. And so you know in Windows I'm kind of like where is that being set. It can be set in a variety of places. And the second point. Right. Pick your battles or as Mike Mansons says this is the subtle art and if you haven't read this the art of not I'm not going to finish this phrase. This is the subtle are not. Forget it I'll say it. This is the subtle art of not giving a [REMOVED] right. This is a fantastic book by the way and all include in our course outline a link for all of the books that I like up here on the course resources. So right here and of course resources all add course resources and other resources including books I like. All at it right there. And but that book it's a great book. And that's the second point is this the subtle art of like picking your battles knowing which battles to choose. And so I'm not sure where that go path environment variable is being set and I don't I don't need to know it's being set somewhere that's fine. I just need to reset it. So right here under system variables I might think well do I want to set the go path where anybody who uses this computer if it's only me using the computer fine. If there's other people using the computer I'm going to want to set the go path for each person logged into the PC. So since this go path this go workspace is set for whoever's that recording PC and that's where the directory is under that person's documents. I'm going to set it up here and I'm just going to do a new new variable and it's going to be go path. And now I need to go get that go path. So here's my workspace and it's right there I'm going to copy that. I clicked in there and then just Control-C to copy it and then paste it. And I'm going to hit. OK. And now I have my go path there and I'm going to hit OK and I'm going to close my bash shell and I'm going to restart it. BASH And I'm going to pop it open and now go in vi and I could see here that my go to see go and my go path is now set to my Go workspace. So I was able to overwrite it. That's what I needed to have done my go path points to my Go workspace. That's what I need for my code to work when I start running my code you know and creating my code. And that's that's setting your go path here go route and that's what environment variables are. So your go route should automatically be set by default to wherever it got installed. But if for some reason that changed. If I go look at c call and backslash go right if I go to my C-Drive open this. Come on baby. Well maybe you open this up go to my C-Drive And there you go. So that's what that one's pointing towards that's where it go installed when I downloaded it. So those are my environment variables you want to make sure that go path points to your go workspace and that go route points to the binary that's been downloaded. If you're on a Mac or Linux you're going to do something like this in your terminal inside a bash or C or bash profil which were these things right here on this copy those and bring that down inside there. You will want to set something like that so you'll open up this file right here. Bassed profile or create it and you'll do nano bash profile and then you'll add this exact code where the go path is pointing towards your workspace. You can also add a path variable to your binary so that you can run things just by typing in their name. And then you'll be like where is that it will go down this path and find it in your binary and run that executable. And that's how you set it up on a Mac or Linux is going to be very similar. All right. So that's your environment variables and getting configured to work with go.