Building a CLI application with Golang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of my life's Pleasures is playing football I do that once a week but as much as I love the game sometimes the weather gets in the way you look up the window see some clouds get worried go on Google search for the weather forecast and pray it won't rain but I'm a developer so I spend a lot of time in the terminal so it would be awesome if I could just type in a command that shows me the forecast well today we're going to make that a reality and we'll do it by using golang and weather API the first thing we'll do is create a new directory for our project we'll City into it and initialize a new go module using go mod init and then the name of the module let's open this up in vs code and create a new file called main.go which will be in our main package we'll have a main function in which we'll simply print out a message to the console we can now open the terminal type in Goron main.go and we'll see our message now to get the weather forecast I have an end point to which we'll need to make a get request so we'll go here and say res error equals http.get and I'll paste in the link here I have my API key and my current location then we'll check for the error so if error does not equal to nil will panic and pass it the error this will stop the execution of the program and print out the error another thing we need to do is once this main function finishes executing we need to close the body of the response and we can do that using defer res body close let's go ahead and check the status code of the response so if response status code does not equal 200 then we'll Panic saying whether API not available let's open up the terminal run the command again we see nothing there but if we change this check right here we should see the error there we go next up we need to read the body of the response so we'll do body error equals IO read all we'll pass it the body just as before we'll check for the error and now let's print out the body this is a slice of byte so we need to convert it to a string we'll do fmt print Ln and then string body let's open up the terminal run the command again and this is our Json response let's copy it I'll create a new file called response.json this is just a temporary file that's format the document and move it to the sides the reason I'm pulling this here is because what we want to do next is take this Json response and convert it to a ghost chart so we'll go here and we'll have something like type weather which will be a struct we'll have location which will be a struct we'll add a Json tag pointing to location inside it we'll have name and Country these are the two properties I need so we'll do name String We'll add the Json tag name we'll add country and then we'll move out to current so let's copy this and say current here we need the temperature and the condition text so we'll have temp c this will be a float 64. Json temp underscore C and then we have condition which is a struct we'll add the tag inside it we'll have text which is a string Json text moving on we have forecast which is an object meaning that here we'll have a forecast struct let's add the Json tag inside it we have forecast day which is an array of objects meaning that we'll have a slice of struct so we'll do a forecast day slice obstruct add the tag in here what we need is the hour array so we'll basically have the same thing here over which is an array of structs from R and from the hour we need the time Epoch so we'll do time epoch this will be an N64 let's add the Json tag and we'll also need a temperature this is a float64 then we'll grab the condition text and the chance of rain where is it chance of rain here so the condition is basically the same as here and chance of rain will be a float 64. chance of rain cloud 64. and here we'll have underscores and this is our ghost truck so basically what we did here is Define how our Json response will be mapped to our weather struct to actually do that we'll need a weather variable so let's go here and say VAR weather of type weather and then use the Json package to unmarshall the body into that variable we need to pass the body and then the address of our variable so this will take the body and convert it into whatever we passed here of course this can also return an error so let's add it here and check it and then go ahead and print out the weather let's run the command again and here it is next up let's extract some variables that are easier to work with we'll do location current and hours and we'll grab them from weather dot location weather dot current and then we'll have weather forecast forecast day this is a slice we'll always have one item so if zero and then hour then we'll need to print out information about the current state of the weather we'll want something like Affinity print F and say Yash Romania which is a country temperature in celsius and then condition let's close this out we no longer need it now all of these will be replaced with values from here so here we'll have a string here we'll have another string this is a float so we'll do dot 0f we want to format it and show no decimals and here we'll have the condition which will be a string now we'll need to pass values for each of those we'll do a location dot name location dot country current temp c and then current condition text let's add a slash n here to go on a new line and underscore because ours is currently unused if we open the terminal and type in go run main.go we'll see information about the current state of the weather next up we need to put the hours back in Loop through them and print out the forecast we'll have four underscore our equals to range of hours and inside it will print out something like our temperature rain and condition just as before we need to replace these with values from the hour here we'll have a string here we'll have a float so we'll do percentage dot zero f and this will be the temperature so Celsius then we'll have the rain which is also a float and condition which is a string now we need to pass values for each of these the first one is the actual hour and we have this time Epoch here we can use but we'll need to create a date first so we'll do a date equals time of Unix we'll pass it the hour timer book and zero and here we can do date format and to show the hour and the minutes we can do 15 and 0 4. next up we have the temperature so we'll do our DOT MC the chances of rain and then we have the condition text and of course this is before let's add a slash n to print out a new line at the end let's run this and here we go one thing would be nice is that we should only display hours that are in the future so let's go ahead and add the condition saying that if the date is before time now skip this using continue now if we run it again we only see hours in the future let's go ahead and add the percentage point for the chances of rain here and another thing I would like is to have like a threshold and if that threshold is exceeded show that Row in red we'll change this into Sprint F and this will only return the formatted string let's store it into a message variable and then add a check for the chance of rain so if our chance of rain is less than 40. we'll print it out using fmt otherwise we'll print it out using a package that allows us to use colors so we'll install this and then we can do color dot red and pass it the message now if we run it we see red everywhere there's a chance more than 40 percent to rain so today is rainy day which is funny because right now it's sunny outside the last thing I want to do is allow the user to pass in a location as argument when running the command so we go here where we have the location and replace it with a variable let's go ahead and set queue default value to Yash and then if the user passes any argument so Len of OS args is bigger or equal to 2 because by default the command name is an argument then we'll set queue to OS arcs of 1. now if we open the terminal and type in go run main.go we'll see the weather for Yash but if we provide an argument let's say London we see the weather for London finally let's build this program using go build and move the sun executable into our user local bin this will make the sun command available everywhere so I can go anywhere in the terminal type in Sun and I'll see the forecast for the current day if I type in Sun and then on the different City I see the forecast for that City and that was it if you enjoyed this video don't forget to like it share it subscribe click the Bell button all that good stuff bye
Info
Channel: cdruc
Views: 34,278
Rating: undefined out of 5
Keywords: golang, go, cli
Id: zPYjfgxYO7k
Channel Id: undefined
Length: 12min 30sec (750 seconds)
Published: Sun May 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.