Complete Go for Professional Developers

Parsing Command-Line Flags

Melkey
Twitch
Complete Go for Professional Developers

Lesson Description

The "Parsing Command-Line Flags" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:

Melkey refactors the main application to accept command-line arguments for the port. The flag package parses the command-line arguments, and if a port argument exists, the server will use that. Otherwise, the default port of 8080 will be used.

Preview
Close

Transcript from the "Parsing Command-Line Flags" Lesson

[00:00:00]
>> Melkey: Next thing I kinda wanna show is just some quality of life features in the funk main with another package. So the standard library and go is super rich. It's like, it's fat. That's the best way I could describe it. There's so many different packages that, again, you don't need any external package, like a purist, a code purist will write everything themselves, which is very awesome, I would say.

[00:00:27]
But because of that it makes a challenge of someone trying to teach code hard. I can't teach everything from the standard library, we'll be here until the next snowstorm alert, but what I do want to do is introduce some packages that may catch your eye, catching interest, right?

[00:00:46]
And I think one that is worth mentioning is the flag package. Okay, so the flag package in go basically allows us to parse flags to be passed in when calling our function. This could be our binary executable file if you create one or just using go run main.go.

[00:01:03]
So here we can declare our variable as port, can be int. We don't need to assign a value right here. But what we can do is use our new flag package and grab int var, okay? And here we're gonna pass it the address of the port because we want to parse the value we are getting from the command into our port integer here, our variable port, which is an integer.

[00:01:30]
We're gonna say port, which is the name of the flag that we will add, okay? And then we'll give it a default value 8080. Meaning, if someone calls our function and don't pass port, we will default this to 8080, and we'll just give it a description. Go backend server port like so, and then don't forget, you have to do flag.parse, okay?

[00:01:53]
This actually calls the parse function, which does all the heavy lifting for us, okay? And then here in address, we can actually fully replace this with format.sprintf. Now, sprintf, very similar to printf, but the s in front of it returns a variable for us. So printf, just quickly, just right away, prints to the standard output.

[00:02:15]
This printf returns a variable for us, okay? And in here, When you're calling ("%d", port), and we'll put in port variable like so. So now when we run we should be able to go back, nuke this, run it again, and you know what? I'm gonna add something, so here I'm wanna do, I wanna move up blogger, put it down here, and I'm gonna say printf, I'm gonna say we are running on port modulo d.

[00:02:55]
>> Melkey: We can put our port variable like so. And here, if you run this, we should see we are running on port 8080 but if here, if we do go, run, main.go, port 8081, oops. Go, run, main.go, port 8081, you should see we are now running on port 8081, right?

[00:03:17]
Key difference here versus here, default value, it's gonna go back to whatever the default we set up here. Otherwise you can specify the port. So that's just some of the abilities that the flag package provides us. So we can use it for distinguishing how we're gonna change the behavior app depending on what the user passes to us.

[00:03:39]
>> Speaker 2: Are there environment variable techniques you can use as well?
>> Melkey: Yeah, there is definitely lots of environmental variable techniques that we can use. There's like the OS packaging go, the built in here has something that you can use. I think it's got OS Getenv like here. So get into, choose a value of the environment variable named by the key.

[00:04:01]
So you can use something like this, but there's actually a package that makes retrieving get environment variables from a .env much easier. So again, it's a function that you'd bring in, or package you'd bring in makes environment variables super easy. And one thing you can do if you want is, you can parse a value from the command line interface.

[00:04:24]
If it's not there, you can default to the OS.Getenv value, or you can replace it. So there's a few things you can do there.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Start a 7-Day Free Trial