go
3 TopicsGo in Plain English: Creating your First WebServer
Related Articles Go in Plain English: Setting things up and Hello World! Go in Plain English: Playing with Strings Quick Intro I'm the kind of guy that I need to get something done to believe I'm learning. In this short article, I'll show you how easy it is to be up and running with a WebServer in Go. The Code Here's the code you need to run a very basic web server: Code Explained We're importing net/http which is the library that allows Go to handle HTTP requests and do something about them. Our responder function here is used to create our HTTP response, i.e. what are we going to respond to web browser once we receive a request. The Fprint function here is particularly useful because it formats whatever you add to second parameter and throws it into first parameter (resp). The function responder becomes will be our response. In the main part of the program, we callhttp.HandleFuncand what it does is it calls function responder if we receive an HTTP request to "/" to handle the HTTP response. http.ListenAndServe makes sure our HTTP server is listening on port 8080. Running our Code I saved my code in draft.go and ran it like this: This is what I see: Adding more Paths I just added an IF and an ELSE IF statements: You need to hit Ctrl+C to stop our previous code and start it again: But here's what we get when we go to /anotherpath: Notice that all responses up to now are 200 OK: If we go to anything else that is not / or /anotherpath we hit our last else statement: Because we explicitly addedhttp.StatusNotFoundthen the response is now 404: That should be enough to get started but this library is very powerful and rich. The documentation along with all functions are here:https://golang.org/pkg/net/http/ Optimising our Code If your page has many paths, it's probably better to organise them into functions. In this case, we can use a router such ashttps://github.com/gorilla/muxbut we need to follow the instructions in previous link to clone library locally so it's available to your installed Go version. After it's all done, we can split the paths into functions and that's way more scalable than using multiple if's and else's: The major difference here is that we need to create an instance of the router first (my router := mux.NewRouter()) and then we add the appropriate paths per function. And finally: Hang on! Why we didn't create a function for any other non-existing paths (404)? Because that's the default:579Views0likes2CommentsGo in Plain English: Setting things up and Hello World!
Related Articles: Go In Plain English: Playing with Strings Go In Plain English: Creating your First WebServer Quick Intro I like to keep things short so just do what I do here and you will pick things up. In this article we'll cover the following: Installation (1 liner) Hello world! The directory structure of Go BONUS! VS Code for Golang Installation →https://golang.org/doc/install Note:If you never programmed in Go, please go straight to the bonus section, install VS Code and come back here! Hello World! At the moment just keep in mind that you'll typically see package, import and func in a Go program. If you're just getting started then this is not the time to go really in-depth but package main signals go that our piece of code is an executable. The import statement is where you tell go which additional package you'd like to import. In this case, we're importing fmt package (the one that has the Printf method). Lastly, func main() is where you're going to add your code to, i.e. the entry point of our program. Running your Go code! You can pick a folder of your choice to run your Go apps. In my case I picked /Users/albuquerque/Documents/go-apps/ directory here: Inside of it, you create a src directory to keep your *.go source code inside: go run When you type go run you're telling Go that you don't care about executable (for now) so go compiles your code, keeps executable into a temporary directory and runs it for you. go build When you type go build, Go compiles the executable and saves it to your current directory: go install There is a variable called $GOPATH where we tell Go where to look for source files. Let's set it to our go-apps directory: When we type go install, Go creates a bin directory to keep the executable: go doc In case you want to know what a particular command does, just use go doc command: BONUS! VS Code for Golang The program I use to code in Go and Python is VS Code: It has autocompletion and roughly all you expect from a proper code editor. You can download it from here:https://code.visualstudio.com/download You can choose themes too:784Views3likes5CommentsGo in Plain English: Playing with Strings
Related Articles Go in Plain English: Creating your First WebServer Quick Intro In this article I'm going to start with Strings to give you some hands-on exposure. You need to install Go first and ideally a code editor like VS Code as shown inGolang in Plain English: Settings things up and Hello World! The way I'll go through variables here is the way I taught myself Go so it shouldn't be hard. Just repeat what I do here and you should be fine. Here's what I'll go through: Declaring a variable in Go (you'll even understand the mysterious :=) Using just parts of Strings with Slicing Iterating through String Characters Printing the Length of a String How do we print double quotes inside of print statement? Declaring a variable in Go This is how we declare a variable in Go: Go makes it easier by letting you both declare and set the variable at the same time by adding a colon before the equals sign like this: However, if I try to run it Go won't let me because it requires that all declared variables must be used: So if we add a print statement it will work: Note that %s means we're printing a string. If it was an integer, for example, you'd use %d instead. It now works: Using just parts of the string with Slicing Let's say you just want to print DevCentral (character 0 to 10) but you don't want to touch the variable. This is what you do: Here's the output: You could've omitted the 0 leaving just my variable[:10] and Go would understand that starting position is 0 anyway. We can also do the other way round if we want to print just awesome (character 14 until the end). This time I'm just going to omit the last position as Go understands it's up to the end: Here's the output: Iterating through string characters The other MUST know is how to go through each character in a for loop using range: The first thing to understand here is that our for loop requires 2 things: the index we're at (0,1,2,3, etc) and the character. So first iteration: i = 0, c = 'D', 2nd iteration: i = 1, c = 'e', and so on. Let's print this out: Let's say you don't care about the index. In that case, Go has something very handy for this. Just use the _ character and Go will ignore it: Here we go: If it's just the index, you don't need the _ because Go already understands that you just want to print the index as it's the first entry (from i,c): And here's the output again: Printing the Length of a String Another useful thing is to learn how to print the length of a string which is not very different from other programming languages: Here's the output: How do we print double quotes inside of print statement? Just put your string between back quotes like this: Let's run it: Oops! It also printed our new line character. Let's fix it by taking it out of our back quotes and merging it between double quotes like this: Let's run it one more time: Fixed!400Views0likes0Comments