{"id":70014,"date":"2017-02-24T12:20:51","date_gmt":"2017-02-24T12:20:51","guid":{"rendered":"https:\/\/www.simple-talk.com\/?p=70014"},"modified":"2021-06-03T16:47:04","modified_gmt":"2021-06-03T16:47:04","slug":"go-language-useful","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/development\/dotnet-development\/go-language-useful\/","title":{"rendered":"What is the Go Language, and Why is it Useful?"},"content":{"rendered":"<h1>What is Go?<\/h1>\n<p>The Go documentation describes Go succinctly:<\/p>\n<p>&#8220;Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It&#8217;s a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.&#8221;<\/p>\n<p>What this means is that it is:<\/p>\n<ol>\n<li>a relatively simple language, certainly compared to c++ and to some extents c#.<\/li>\n<li>easy to write programs that scale on multicores<\/li>\n<li>networked software<\/li>\n<li>fast to compile, even the largest applications take seconds to compile<\/li>\n<li>statically typed for safety<\/li>\n<\/ol>\n<p>If you are going to write something that will serve network requests of some sort, and will need to scale to a significant numbers of requests, then I would say that Go should be one of the languages that you ought to consider using. Furthermore, if you are looking for a cross platform language that compiles to a console application which will run on the target machine without any dependencies, you also can&#8217;t go too far wrong in looking at using Go.<\/p>\n<h1>What is the history?<\/h1>\n<p>Go was invented by three engineers at Google, supposedly impatient at the time it took to build the C++ source at Google. The idea was to build a replacement for C++ that would mean that where they had chosen between C++ for performance with both its complexity and slow build times, and something like Java or Python. The inventors decided that a new language was the only way forward, and that language ended up as Go.<\/p>\n<h1>What is it best suited to?<\/h1>\n<p>Go is best suited to networked services that need to scale. Go has a number of features that mean that it is well-suited to building applications that have to be fast. The first of these &#8216;turbo&#8217; features is Go&#8217;s ability to scale across multiple cores by making multi-threaded programming straightforward. Go uses a model for concurrency called CSP or &#8216;Communicating Sequential Processes&#8217;. The idea is that, instead of sharing variables and having to control access to those variables, shared values are passed between threads using channels. If only one thread can access a value at a given time then you don&#8217;t need to lock the variable. The Go authors have reduced this to a slogan:<\/p>\n<h3>&#8220;Do not communicate by sharing memory; instead, share memory by communicating.&#8221;<\/h3>\n<p>In a practical sense what this slogan means is that, instead of doing it by using locks, mutexes or ReadWriterLockSlims, we pass the object that contains the variable from one <code><span style=\"color: #444444;\"> GoRoutine<\/span><\/code> (akin to a thread) to another &#8211; you use the object and then, when you are finished, you pass it onto the next channel or throw it away.<\/p>\n<h1>Cross Platform<\/h1>\n<p>A further benefit of Go is that it is a cross-platform language and has a runtime that works on Windows, FreeBSD, Mac OS X and Linux. When you compile your Go binary, the runtime is compiled into the executable and that can be copied onto a machine and run &#8211; there is no need to run an installer or get any dependencies for your application to run. Go also includes support for building on one architecture but targeting another so from a 32-bit Windows XP machine, should you be unfortunate enough to have a Windows XP machine, you can build a 64-bit binary to run on a Linux machine.<\/p>\n<h1>Installing Go<\/h1>\n<p>To get going with Go you need to install it on your development machine, the latest version can be downloaded from:<\/p>\n<p style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/golang.org\/dl\/\"> https:\/\/golang.org\/dl\/<\/a><\/p>\n<p>Once you have downloaded and either installed the MSI on Windows or extracted the <code><span style=\"color: #444444;\">.tar.gz<\/span><\/code> on <code><span style=\"color: #444444;\">Linx\/FreeBSD\/Mac<\/span><\/code>, you need to set a couple of environment variables:<\/p>\n<p>The first environment variable is <code> <span style=\"color: #444444;\">GOROOT<\/span><\/code> which needs to be set to the value of the path to the root installation folder, assuming you installed to &#8220;<code><span style=\"color: #444444;\">c:\\go<\/span><\/code>&#8221; on windows or &#8220;<code><span style=\"color: #444444;\">~\/go<\/span><\/code>&#8221; then you would use the console command:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><code><span style=\"color: #444444;\">\"set GOROOT=c:\\Go\" or \"export GOROOT=$HOME\/go\"<\/span><\/code><\/div>\n<p>You should also update your path variable so that it includes the <code> <span style=\"color: #444444;\">\"%GOROOT%\/bin\"<\/span><\/code> or <code><span style=\"color: #444444;\">\"$GOROOT\/bin<\/span><\/code>&#8221; folder.<\/p>\n<p>The next step is to set up your workspace folder, which stores downloaded packages etc. and source code. The environment variable &#8220;<code><span style=\"color: #444444;\">GOPATH<\/span><\/code><span style=\"color: #444444;\">&#8221; should point to that folder:<\/span><\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">&#8220;<code><span style=\"color: #444444;\">set GOPATH=c:\\Users\\User.Name\\Documents\\gocode\" or \"export GOPATH=$HOME\/gocode<\/span><\/code><span style=\"color: #444444;\">&#8220;<\/span><\/p>\n<\/div>\n<p>To test that you have everything set up correctly, you should open a command prompt and type &#8220;<code><span style=\"color: #444444;\">go env<\/span><\/code>&#8220;, and make sure you have a value under <code> <span style=\"color: #444444;\">GOROOT<\/span><\/code> and <code><span style=\"color: #444444;\">GOPATH<\/span><\/code>.<\/p>\n<h1>Our first Go Program<\/h1>\n<p>If you create a new folder under &#8220;%GOPATH%&#8221; or &#8220;$GOPATH&#8221; and call it &#8220;src&#8221;, then inside that create a folder called &#8220;simple-talk&#8221; and inside that create a text file called &#8220;main.go&#8221; and write:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">package<\/span><span style=\"color: #444444;\"> main<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">import<\/span> <span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">fmt<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">main<\/span><span style=\"color: #444444;\">(){<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">Hello Simple Talk!<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/play.golang.org\/p\/Uet9PBWaCo\"> https:\/\/play.golang.org\/p\/Uet9PBWaCo<\/a><\/p>\n<p>Save this and from the same directory run &#8220;go build&#8221; which should run without any errors and complete within a second or so. If you are on Windows you will have a &#8220;simple-talk.exe&#8221; or on other platforms a &#8220;simple-talk&#8221; executable. If you run the executable it should give you the output &#8220;Hello Simple Talk!&#8221;.<\/p>\n<p>If we take this line-by-line, we have:<\/p>\n<p>&#8220;<code><span style=\"color: #444444;\">package main<\/span><\/code>&#8221; This is like a namespace in C#, but differs from a namespace in C# in that you can&#8217;t put whatever you like as the package name. An application always starts with &#8220;package main&#8221; and most smaller programs do not use any other packages: Instead, packages are used to load libraries to control visibility outside of the library.<\/p>\n<p>The next line is &#8216;<code><span style=\"color: #444444;\">import \"fmt\"<\/span><\/code>&#8216;. This is importing the &#8220;<code><span style=\"color: #444444;\">fmt<\/span><\/code>&#8221; package which we are using to print out some text: In Go you need to import the name of any packages that you are going to use. Import is like adding a reference to a dll and using the using keyword in C#. When you import a package, the targeted package is compiled into the Go binary; so if you import a package that you do not use, the compiler will throw an error and force you to &#8220;use it or lose it&#8221;.<\/p>\n<p>We then have &#8220;<code><span style=\"color: #444444;\">func main(){<\/span><\/code>&#8221; which is the start of the main func &#8211; main is the entry point to every Go app. In this line we also see that Go is based on the C style of languages, preferring to use cURLy braces to separate code rather than whitespace or keywords.<\/p>\n<p>The second to last line is:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><code><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0`fmt.Println(\"Hello Simple Talk!\")`<\/span><\/code><\/div>\n<p>This uses the &#8220;<code><span style=\"color: #444444;\">Println<\/span><\/code>&#8221; method which is in the &#8220;<code><span style=\"color: #444444;\">fmt<\/span><\/code>&#8221; package and simply prints out a string. The last line is the cURLy brace that ends the method.<\/p>\n<p>The first of a couple of points here is the lack of semi-colons anywhere, Go does use semi-colons and while you can end a line with one, the standard Go tools will, if configured, remove the semi-colons. When the code is compiled, semi-colons are added for you but are only seen by the compiler. This means that your source code is mostly free of semi-colons in order to aid readability.<\/p>\n<p>The second point is that this is exactly how this Go program should look: The writers of Go are very opinionated and say what they consider to be &#8220;Idiomatic Go&#8221; and what isn&#8217;t &#8211; anything that isn&#8217;t idiomatic is normally wrong. Although I use semi-colons in JavaScript and it feels a little mean being told how to layout my code, it actually makes life a lot easier because every sample on the internet is the same, the code in the standard libraries acts and looks the same and it means you don&#8217;t end up with different coding standards and styles.<\/p>\n<h1>Go toolchain<\/h1>\n<h2>go fmt<\/h2>\n<p>The command to build Go programs is &#8220;go build&#8221;. There are also other commands such as &#8220;go fmt&#8221; which takes your source code and re-formats it in the Go style. If we took this as an example &#8230;<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">package<\/span><span style=\"color: #444444;\"> main;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">import<\/span> <span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">fmt<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">main<\/span><span style=\"color: #444444;\">() { <\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">Hello Simple Talk!<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">);<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p>&#8230; we can see that we have been a little frugal with whitespace and a little over the top with semi-colons. By running &#8220;go fmt&#8221; in the same directory, you will be able to re-format the code as:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">package<\/span><span style=\"color: #444444;\"> main<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">import<\/span> <span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">fmt<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">main<\/span><span style=\"color: #444444;\">() {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">Hello Simple Talk!<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<h2>go vet<\/h2>\n<p>Code can also be validated at compile time to find common issues that were not found by the compiler, such as <code><span style=\"color: #444444;\"> Printf<\/span><\/code> calls that do not match the format string. I have personally seen many many issues in C# that were caused by null reference exceptions in String.Format or some variant.<\/p>\n<p>If we change our sample code so it looks like this (note the missing value which should be passed into Printf after the format string) &#8230;<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">package<\/span><span style=\"color: #444444;\"> main<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">import<\/span> <span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">fmt<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">main<\/span><span style=\"color: #444444;\">() {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>fmt.<\/span><span style=\"color: #0086b3;\">Printf<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">Hello Simple Talk! My name is <\/span><span style=\"color: #0086b3;\">%v<\/span><span class=\"pl-cce2\"><span style=\"color: #a71d5d;\">\\n<\/span><\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/play.golang.org\/p\/28WdebLPnc\"> https:\/\/play.golang.org\/p\/28WdebLPnc<\/a><\/p>\n<p>&#8230;then running &#8220;go vet&#8221; in the folder gives us this error:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><code><span style=\"color: #444444;\">test.go:6: missing argument for Printf(\"%v\"): format reads arg 1, have only 0 args<\/span><\/code><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><code><span style=\"color: #444444;\">exit status 1<\/span><\/code><\/p>\n<\/div>\n<p>Although, in Go, this wouldn&#8217;t be a runtime error, it wouldn&#8217;t actually log a value. This would be very annoying to find out when you are debugging an issue and you see the text &#8220;Hello Simple Talk! My name is %!v(MISSING)&#8221; so it is nice to have tooling &#8216;out-of-the-box&#8217; that can help stop these sort of silly errors.<\/p>\n<h1>IDEs<\/h1>\n<p>There are a number of IDEs that are available for Go, I am using Visual Studio Code with the Go extension but Jetbrains have an IDE and there are a few other ones as well:<\/p>\n<ul>\n<li>Visual Studio Code &#8211; <a href=\"https:\/\/code.visualstudio.com\/\">https:\/\/code.visualstudio.com\/<\/a><\/li>\n<li>IntelliJ Go &#8211; <a href=\"http:\/\/www.jetbrains.com\/idea\/\">http:\/\/www.jetbrains.com\/idea\/<\/a><\/li>\n<li>A full list of IDE&#8217;s and plugins: <a href=\"https:\/\/github.com\/golang\/go\/wiki\/IDEsAndTextEditorPlugins\">https:\/\/github.com\/golang\/go\/wiki\/IDEsAndTextEditorPlugins<\/a><\/li>\n<\/ul>\n<h1>Language Features of note<\/h1>\n<p>We will finish the first part of the introduction to Go for C# developers with a quick overview of a couple of features of Go which make developing a real pleasure.<\/p>\n<h2>Multiple return values<\/h2>\n<p>In Go, functions can return multiple values. This allows us to return both an object and a status for example; which means we can save a lot of the cruft that we get in other languages. If we look at an example of returning multiple values in C# we might create a <code><span style=\"color: #444444;\"> struct<\/span><\/code> to return the data:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">class<\/span> <span style=\"color: #795da3;\">Something<\/span><span style=\"color: #444444;\">{<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/span><span style=\"color: #a71d5d;\">public<\/span> <span style=\"color: #795da3;\">SomethingResult<\/span> <span style=\"color: #795da3;\">GoOn<\/span><span style=\"color: #444444;\">(){<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/span><span style=\"color: #a71d5d;\">var<\/span> <span style=\"color: #795da3;\">result <\/span><span style=\"color: #444444;\">= <\/span><span style=\"color: #a71d5d;\">new<\/span> <span style=\"color: #795da3;\">SomethingResult<\/span><span style=\"color: #444444;\">();<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/span><span style=\"color: #a71d5d;\">if<\/span><span style=\"color: #444444;\">(itDidIt()){<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>result.SomeObject = <\/span><span style=\"color: #a71d5d;\">new<\/span> <span style=\"color: #795da3;\">SomeObject<\/span><span style=\"color: #444444;\">();<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>result.Success = <\/span><span style=\"color: #0086b3;\">true<\/span><span style=\"color: #444444;\">;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/span><span style=\"color: #a71d5d;\">return<\/span><span style=\"color: #444444;\"> result;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>result.Success = <\/span><span style=\"color: #0086b3;\">false<\/span><span style=\"color: #444444;\">;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/span><span style=\"color: #a71d5d;\">return<\/span><span style=\"color: #444444;\"> result;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p>To do the same thing in Go we could write it like:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/span><span style=\"color: #a71d5d;\">if<\/span> <span style=\"color: #0086b3;\">itDidIt<\/span><span style=\"color: #444444;\">() {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/span><span style=\"color: #a71d5d;\">return<\/span> <span style=\"color: #0086b3;\">true<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #0086b3;\">new<\/span><span style=\"color: #444444;\">(someObject)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">return<\/span> <span style=\"color: #0086b3;\">false<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #0086b3;\">nil<\/span><\/p>\n<\/div>\n<p style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/play.golang.org\/p\/x2R5ti9xWH\"> https:\/\/play.golang.org\/p\/x2R5ti9xWH<\/a><\/p>\n<p>This leads to much easier to read code and avoids horrible alternatives in C# such as:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">public<\/span> <span style=\"color: #a71d5d;\">void<\/span> <span style=\"color: #795da3;\">multipleReturnValuesAsOutArgs<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #a71d5d;\">int<\/span> <span class=\"pl-smi1\">value<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #a71d5d;\">out<\/span> <span style=\"color: #a71d5d;\">bool<\/span> <span class=\"pl-smi1\">status<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #a71d5d;\">out<\/span> <span style=\"color: #795da3;\">someObject<\/span> <span class=\"pl-smi1\">someObject<\/span><span style=\"color: #444444;\">){<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>&#8230;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">public<\/span> <span style=\"color: #a71d5d;\">void<\/span><span style=\"color: #444444;\"> call <\/span><span style=\"color: #795da3;\">it<\/span><span style=\"color: #444444;\">(){<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/span><span style=\"color: #a71d5d;\">var<\/span> <span style=\"color: #795da3;\">status <\/span><span style=\"color: #444444;\">= <\/span><span style=\"color: #0086b3;\">false<\/span><span style=\"color: #444444;\">;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/span><span style=\"color: #795da3;\">someObject<\/span> <span style=\"color: #795da3;\">someObject<\/span><span style=\"color: #444444;\"> = <\/span><span style=\"color: #0086b3;\">null<\/span><span style=\"color: #444444;\">;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>multipleReturnValuesAsOutArgs(<\/span><span style=\"color: #0086b3;\">999<\/span><span style=\"color: #444444;\">, out status, out someObject);<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/span><span style=\"color: #a71d5d;\">if<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #795da3;\">status<\/span><span style=\"color: #444444;\">){<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/span><span class=\"pl-c2\">\/\/blah blah blah<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>} <\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p>This doesn&#8217;t seem like a big deal, but when you are programming it allows you to quickly get on with the important and interesting thing if you can quickly return several different values from a function.<\/p>\n<h2>Garbage collection<\/h2>\n<p>The last language feature I will talk about today is the garbage collection. C# will let you create objects on the stack or the heap depending on the object type and garbage collection is quite expensive. In C#, value types such as ints, DateTimes or other structs are created on the stack and classes and other reference types are created on the heap; if you have a method that, in a tight loop, instantiates and then throws away significant amounts of objects, it will cause a lot of heap allocations and contention. This will make a garbage collection more likely. In Go, the compiler looks at how objects are used and, where possible, objects are created on the stack; this can make a massive difference to garbage collection. So even though Go is a garbage-collected langage like C#, this is not something to fear &#8211; the performance of it is awesome.<\/p>\n<h1>More Exciting GO<\/h1>\n<p>In the first part of this article, we showed you how to get setup with a build environment for GO and how to build a simple app. In this part we will look at doing something a little more exciting. We will demonstrate what Go is good for: a multi-threaded, scalable, networked service &#8211; an API that can be used as a replacement for <a href=\"http:\/\/isitlunchtimeyet.com\/\"> http:\/\/isitlunchtimeyet.com\/<\/a>. We will show how to create a simple web application to serve the response to the critical question &#8220;Is It Lunchtime yet?&#8221;.<\/p>\n<h1>References<\/h1>\n<p>Each compiled go program includes its own compiled source code and, in addition, the compiled source code of every go dependency linked into the executable. To add a reference to a dependency you import the package that the dependency is in; so, for example, if you wanted to print out the current time, and you wanted to use:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">The time is now: <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, time.<\/span><span style=\"color: #0086b3;\">Now<\/span><span style=\"color: #444444;\">(), <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\"> in UTC it is: <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, time.<\/span><span style=\"color: #0086b3;\">Now<\/span><span style=\"color: #444444;\">().<\/span><span style=\"color: #0086b3;\">UTC<\/span><span style=\"color: #444444;\">())<\/span><\/p>\n<\/div>\n<p>you would need to import the &#8220;time&#8221; and &#8220;fmt&#8221; packages. To do that, simply use an &#8220;import&#8221; statement or an &#8220;import&#8221; block:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">import<\/span> <span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">time<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">import<\/span> <span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">fmt<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<\/div>\n<p>\u2026 or the common syntax is:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">import<\/span><span style=\"color: #444444;\">(<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">fmt<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">time<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">)<\/span><\/p>\n<\/div>\n<p style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/play.golang.org\/p\/HOSowhuefX\"> https:\/\/play.golang.org\/p\/HOSowhuefX<\/a><\/p>\n<p>This means we can import any part of the GO standard library which is pretty vast:<\/p>\n<p style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/golang.org\/pkg\/\"> https:\/\/golang.org\/pkg\/<\/a><\/p>\n<p>There are other times, though, when we want to either share packages between projects internally or use somebody else&#8217;s package:<span style=\"mso-spacerun: yes;\">\u00a0 <\/span>To do that, we use &#8220;go get&#8221; which:<\/p>\n<ul>\n<li>Fetches a go package from a source control repository like github<\/li>\n<li>Places the source code in a unique directory under the first GOPATH directory<\/li>\n<li>Builds the source code and stores the output so it can be compiled into your project<\/li>\n<\/ul>\n<p>&#8220;go get&#8221; can either be used to fetch any packages you have specified in your &#8220;import&#8221; directives or you can tell it what package to go and get as it is also used to download some useful tools. For example if we had an app that used the \u2018gorilla\u2019 package which is an http routing framework, we would just need to add the import to our main.go program and then run &#8220;go get&#8221; in the same directory as the main.go file<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">package<\/span><span style=\"color: #444444;\"> main<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">import<\/span><span style=\"color: #444444;\"> (<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">github.com\/gorilla\/mux<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">main<\/span><span style=\"color: #444444;\">() {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0 mux.<\/span><span style=\"color: #0086b3;\">NewRouter<\/span><span style=\"color: #444444;\">().<\/span><span style=\"color: #0086b3;\">StrictSlash<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #0086b3;\">true<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p>If we then changed to the directory and typed &#8220;go get&#8221; it would go off and get the code from github.com\/gorilla\/mux build it and we can use it in our code.<\/p>\n<p>There is one thing to be aware of: When you create your directory, it needs to be under a directory that is specified in the environment variable for GOPATH, if you do not then you will see this error when running go get:<\/p>\n<p>go install: no install location for directory &#8220;DIRECTORY&#8221; outside GOPATH<br \/>\n For more details see: go help gopath<\/p>\n<p>If your <code><span style=\"color: #444444;\">GOPATH<\/span><\/code> is set to &#8220;<code><span style=\"color: #444444;\">\/home\/user\/go<\/span><\/code>&#8221; or &#8220;<code><span style=\"color: #444444;\">c:\\users\\user\\go<\/span><\/code>&#8221; then add your directory under &#8220;<code><span style=\"color: #444444;\">\/home\/user\/go\/src\/YourDirectory<\/span><\/code>&#8221; or &#8220;<code><span style=\"color: #444444;\">C:\\users\\user\\go\\src\\YourDirectory<\/span><\/code>&#8221; and you will be fine.<\/p>\n<p style=\"margin-bottom: .0001pt;\"><span style=\"font-family: 'Georgia',serif; color: #444444;\">There are plans to change this and move to more of a project model but as of writing go doesn&#8217;t support the new model yet. There are also some projects which have been created to make this simpler, a good example is <\/span><a href=\"https:\/\/getgb.io\/\"> <span style=\"font-family: 'Georgia',serif;\">https:\/\/getgb.io\/<\/span><\/a><span style=\"color: #444444;\">. For more information on GOPATH, and why it is the way it is, see the blog post by Dave Cheney (<\/span><a href=\"https:\/\/dave.cheney.net\/2016\/12\/20\/thinking-about-gopath\">https:\/\/dave.cheney.net\/2016\/12\/20\/thinking-about-gopath<\/a>)<\/p>\n<p>If we just wanted to do a <b style=\"font: 400 0.9em Consolas,'Courier New',Courier,monospace;\">go get<\/b> without having a local file, then we can pass in the reference to <b style=\"font: 400 0.9em Consolas,'Courier New',Courier,monospace;\">go get <\/b>such as:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><code><span style=\"color: #444444;\">go get github.com\/gorilla\/mux<\/span><\/code><\/p>\n<\/div>\n<h1>Writing a simple http server<\/h1>\n<p>go includes, in its standard library, a fast web server that can serve http or https traffic. For our first example, we will respond to any URL that we receive with a simple text response:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">package<\/span><span style=\"color: #444444;\"> main<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">import<\/span><span style=\"color: #444444;\"> (<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">fmt<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">log<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">net\/http<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">main<\/span><span style=\"color: #444444;\">() {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0http.<\/span><span style=\"color: #0086b3;\">HandleFunc<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">\/<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #0086b3;\">func<\/span><span style=\"color: #444444;\">(w http.<\/span><span class=\"pl-smi1\">ResponseWriter<\/span><span style=\"color: #444444;\">, r *http.<\/span><span class=\"pl-smi1\">Request<\/span><span style=\"color: #444444;\">) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Fprint<\/span><span style=\"color: #444444;\">(w, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">Hello, Simple Talk<\/span><span class=\"pl-cce2\"><span style=\"color: #a71d5d;\">\\n<\/span><\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0})<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0log.<\/span><span style=\"color: #0086b3;\">Fatal<\/span><span style=\"color: #444444;\">(http.<\/span><span style=\"color: #0086b3;\">ListenAndServe<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">:8080<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #0086b3;\">nil<\/span><span style=\"color: #444444;\">))<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/play.golang.org\/p\/umMqWwPxkj\"> https:\/\/play.golang.org\/p\/umMqWwPxkj<\/a> (These won&#8217;t actually run in the playground as it is a restricted\/locked down version)<\/p>\n<p>The important part here is:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\"><span style=\"mso-spacerun: yes;\">\u00a0\u00a0\u00a0\u00a0<\/span>http.<\/span><span style=\"color: #0086b3;\">HandleFunc<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">\/<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #0086b3;\">func<\/span><span style=\"color: #444444;\">(w http.<\/span><span class=\"pl-smi1\">ResponseWriter<\/span><span style=\"color: #444444;\">, r *http.<\/span><span class=\"pl-smi1\">Request<\/span><span style=\"color: #444444;\">) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Fprint<\/span><span style=\"color: #444444;\">(w, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">Hello, Simple Talk<\/span><span class=\"pl-cce2\"><span style=\"color: #a71d5d;\">\\n<\/span><\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0})<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0log.<\/span><span style=\"color: #0086b3;\">Fatal<\/span><span style=\"color: #444444;\">(http.<\/span><span style=\"color: #0086b3;\">ListenAndServe<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">:8080<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #0086b3;\">nil<\/span><span style=\"color: #444444;\">))<\/span><\/p>\n<\/div>\n<p><code><span style=\"color: #444444;\">http.HandleFunc<\/span><\/code> creates a handler for any URL that matches &#8220;\/&#8221; &#8211; and then calls the anonymous function, we could also have split out the function and passed a pointer to it such as \u2026<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">main<\/span><span style=\"color: #444444;\">(){<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0http.<\/span><span style=\"color: #0086b3;\">HandleFunc<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">\/<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, rootHandler)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0log.<\/span><span style=\"color: #0086b3;\">Fatal<\/span><span style=\"color: #444444;\">(http.<\/span><span style=\"color: #0086b3;\">ListenAndServe<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">:8080<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #0086b3;\">nil<\/span><span style=\"color: #444444;\">))<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">rootHandler<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">w<\/span> <span class=\"pl-v2\">http<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">ResponseWriter<\/span><span style=\"color: #444444;\">, <\/span><span class=\"pl-v2\">r<\/span><span style=\"color: #444444;\"> *<\/span><span class=\"pl-v2\">http<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">Request<\/span><span style=\"color: #444444;\">) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Fprint<\/span><span style=\"color: #444444;\">(w, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">Hello, Simple Talk<\/span><span class=\"pl-cce2\"><span style=\"color: #a71d5d;\">\\n<\/span><\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p>\u2026 but an anonymous function is normally fine for a simple function like this.<\/p>\n<p>The next line starts the http server (Listen and Serve) listening on port 8080. If it fails, it will cause the log package to throw a fatal error which, unless caught, will crash the app:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">log.<\/span><span style=\"color: #0086b3;\">Fatal<\/span><span style=\"color: #444444;\">(http.<\/span><span style=\"color: #0086b3;\">ListenAndServe<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">:8080<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #0086b3;\">nil<\/span><span style=\"color: #444444;\">))<\/span><\/p>\n<\/div>\n<p style=\"margin-bottom: .0001pt;\"><span style=\"font-family: 'Georgia',serif; color: #444444;\">If you send a web request to <\/span> <code> http:\/\/localhost:8080<\/code>, you should see the message &#8220;Hello, Simple Talk&#8221;<\/p>\n<p>If we expand on this slightly we can quickly see how easy it is to start to do something useful in go:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">package<\/span><span style=\"color: #444444;\"> main<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">import<\/span><span style=\"color: #444444;\"> (<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">fmt<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">net\/http<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">time<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">main<\/span><span style=\"color: #444444;\">() {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0http.<\/span><span style=\"color: #0086b3;\">HandleFunc<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">\/isitlunchtimeyet<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, handleLunchTimeRequest)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0http.<\/span><span style=\"color: #0086b3;\">ListenAndServe<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">:8080<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #0086b3;\">nil<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">handleLunchTimeRequest<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">res<\/span> <span class=\"pl-v2\">http<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">ResponseWriter<\/span><span style=\"color: #444444;\">, <\/span><span class=\"pl-v2\">req<\/span><span style=\"color: #444444;\"> *<\/span><span class=\"pl-v2\">http<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">Request<\/span><span style=\"color: #444444;\">) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">now<\/span> <span style=\"color: #a71d5d;\">:=<\/span><span style=\"color: #444444;\"> time.<\/span><span style=\"color: #0086b3;\">Now<\/span><span style=\"color: #444444;\">()<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">if<\/span><span style=\"color: #444444;\"> now.<\/span><span style=\"color: #0086b3;\">Hour<\/span><span style=\"color: #444444;\">() &gt; <\/span><span style=\"color: #0086b3;\">11<\/span><span style=\"color: #444444;\"> &amp;&amp; now.<\/span><span style=\"color: #0086b3;\">Hour<\/span><span style=\"color: #444444;\">() &lt; <\/span><span style=\"color: #0086b3;\">14<\/span><span style=\"color: #444444;\"> {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Fprintln<\/span><span style=\"color: #444444;\">(res, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">yes<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">return<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Fprintln<\/span><span style=\"color: #444444;\">(res, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">no<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p>Here, when we are asked for the URL &#8220;\/isitlunchtimeyet&#8221;, if it is between 11am and 2pm we say it is lunchtime, otherwise it isn&#8217;t.<\/p>\n<p>This doesn&#8217;t initially look very scalable but what happens behind the scenes is for each new request a new goroutine is created. A goroutine is analogous to a thread in that each request happens independently and concurrently but is much more lightweight than an actual operating system thread so scaling multiple concurrent goroutines uses much less resources that scaling multiple threads.<\/p>\n<h1>Goroutine<\/h1>\n<p>To use a goroutine, we simply call a function and precede it with the word &#8220;go&#8221;:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">someThing<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">parameterOne<\/span> <span class=\"pl-v2\">string<\/span><span style=\"color: #444444;\">, <\/span><span class=\"pl-v2\">parameterTwo<\/span> <span class=\"pl-v2\">int<\/span><span style=\"color: #444444;\">){<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">parameterOne: <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, parameterOne, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\"> parameterTwo: <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, parameterTwo)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">main<\/span><span style=\"color: #444444;\">(){<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">calling someThing<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">go<\/span> <span style=\"color: #0086b3;\">someThing<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">aaaa<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #0086b3;\">1980<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">done calling someThing<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/play.golang.org\/p\/0u-_70PVLS\"> https:\/\/play.golang.org\/p\/0u-_70PVLS<\/a><\/p>\n<p>When this executes it will cause the <code> <span style=\"color: #444444;\">someThing<\/span><\/code> function to run in a separate <code> <span style=\"color: #444444;\">goroutine<\/span><\/code> to the main function. This will mean that the order of the <code> <span style=\"color: #444444;\">fmt.Println<\/span><\/code><span style=\"color: #444444;\"> will be, &#8220;calling someThing&#8221; followed by one of the other two <\/span><code><span style=\"color: #444444;\">Println<\/span><\/code> statements, because a goroutine is analogous to a thread it isn&#8217;t possible to say exactly which one will come next but they will both be printed &#8211; also in this example because main is going to end which stops the program executing, it isn&#8217;t even possible to say whether the Println in someThing will even be called and in this specific example the main <code> <span style=\"color: #444444;\">goroutine<\/span><\/code><span style=\"color: #444444;\"> will likely exit before <\/span><code> <span style=\"font-size: 10.0pt; color: #444444;\">someThing<\/span><\/code><span style=\"color: #444444;\"> gets a chance to run.<\/span><\/p>\n<p>If we wanted to get the main function to wait for the <code> <span style=\"color: #444444;\">someThing<\/span><\/code><span style=\"color: #444444;\"> goroutine to finish we would need to use a &#8220;channel&#8221; to pass a message to tell the main goroutine that it is safe to finish. Channels are how goroutine&#8217;s communicate and going back to the original slogan from the first part of this article:<\/span><\/p>\n<h3>&#8220;Do not communicate by sharing memory; instead, share memory by communicating.&#8221;<\/h3>\n<p>In c# where we would need to lock an object or sharing a mutex or some other synchronization object between the two threads, we create a channel, tell the main <code> <span style=\"color: #444444;\">goroutine<\/span><\/code> to wait until we send it a message that it is ready. The full example is:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">package<\/span><span style=\"color: #444444;\"> main<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">import<\/span><span style=\"color: #444444;\"> (<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">fmt<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">someThing<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">parameterOne<\/span> <span class=\"pl-v2\">string<\/span><span style=\"color: #444444;\">, <\/span><span class=\"pl-v2\">parameterTwo<\/span> <span class=\"pl-v2\">int<\/span><span style=\"color: #444444;\">, <\/span><span class=\"pl-v2\">exitChannel<\/span> <span class=\"pl-v2\">chan<\/span> <span class=\"pl-v2\">bool<\/span><span style=\"color: #444444;\">) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">parameterOne: <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, parameterOne, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\"> parameterTwo: <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, parameterTwo)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0exitChannel <\/span><span style=\"color: #a71d5d;\">&lt;-<\/span> <span style=\"color: #0086b3;\">true<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">main<\/span><span style=\"color: #444444;\">() {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">exitChannel<\/span> <span style=\"color: #a71d5d;\">:=<\/span> <span style=\"color: #0086b3;\">make<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #a71d5d;\">chan<\/span> <span style=\"color: #a71d5d;\">bool<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">calling someThing<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">go<\/span> <span style=\"color: #0086b3;\">someThing<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">aaaa<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #0086b3;\">1980<\/span><span style=\"color: #444444;\">, exitChannel)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">done calling someThing<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">waiting for stragglers to exit<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">&lt;-<\/span><span style=\"color: #444444;\">exitChannel<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">and now (finally) exiting<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/play.golang.org\/p\/0f5D7xvIxv\"> https:\/\/play.golang.org\/p\/0f5D7xvIxv<\/a><\/p>\n<p>What we have here is we create the channel using:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">exitChannel<\/span> <span style=\"color: #a71d5d;\">:=<\/span> <span style=\"color: #0086b3;\">make<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #a71d5d;\">chan<\/span> <span style=\"color: #a71d5d;\">bool<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<\/div>\n<p>This means create a new variable called <code> <span style=\"color: #444444;\">exitChannel<\/span><\/code> and make it point to a new channel that accepts a single parameter of type bool &#8211; you can pass any type down a channel either a built-in type or your own type. We then have to change the definition of the <code> <span style=\"color: #444444;\">someThing<\/span><\/code> function to also take a channel as a parameter (we have to share some objects!).<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">someThing<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">parameterOne<\/span> <span class=\"pl-v2\">string<\/span><span style=\"color: #444444;\">, <\/span><span class=\"pl-v2\">parameterTwo<\/span> <span class=\"pl-v2\">int<\/span><span style=\"color: #444444;\">, <\/span><span class=\"pl-v2\">exitChannel<\/span> <span class=\"pl-v2\">chan<\/span> <span class=\"pl-v2\">bool<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<\/div>\n<p>In the main function, when we have done everything we need to and we want to wait for the <code><span style=\"color: #444444;\">someThing<\/span><\/code> goroutine, we use:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">&lt;-<\/span><span style=\"color: #444444;\"> exitChannel<\/span><\/p>\n<\/div>\n<p>This means that we want to receive something from the channel. The arrows indicate the direction of flow, either to pull data from the channel or, in the case of the <code><span style=\"color: #444444;\">someThing<\/span><\/code> function, when we are finished we push a value into the channel:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0exitChannel <\/span><span style=\"color: #a71d5d;\">&lt;-<\/span> <span style=\"color: #0086b3;\">true<\/span><\/p>\n<\/div>\n<p>We could have just as easily passed in <b style=\"font: 400 0.9em Consolas,'Courier New',Courier,monospace;\">false<\/b> for this because the main function isn&#8217;t actually looking at the value. If we wanted to store the value we received from the channel, we could do something along the lines of:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">result<\/span> <span style=\"color: #a71d5d;\">:=<\/span><span style=\"color: #444444;\"> exitChannel<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">the exit channel sent us the message<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">: result)<\/span><\/p>\n<\/div>\n<p style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/play.golang.org\/p\/oSKUk3h4TZ\"> https:\/\/play.golang.org\/p\/oSKUk3h4TZ<\/a><\/p>\n<p>In our <code><span style=\"color: #444444;\">isitlunchtimeyet<\/span><\/code> example, it would be quite good if we had a global counter to show how many people had asked whether it was lunch so we can expand our example to include a channel to send each request to so we can keep a count:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">main<\/span><span style=\"color: #444444;\">() {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">countingChannel<\/span> <span style=\"color: #a71d5d;\">:=<\/span> <span style=\"color: #0086b3;\">make<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #a71d5d;\">chan<\/span> <span style=\"color: #a71d5d;\">int<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">go<\/span> <span style=\"color: #0086b3;\">keepCount<\/span><span style=\"color: #444444;\">(countingChannel)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0http.<\/span><span style=\"color: #0086b3;\">HandleFunc<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">\/isitlunchtimeyet<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, handleLunchTimeRequest)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0http.<\/span><span style=\"color: #0086b3;\">ListenAndServe<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">:8080<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #0086b3;\">nil<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">keepCount<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">countingChannel<\/span> <span class=\"pl-v2\">chan<\/span> <span class=\"pl-v2\">int<\/span><span style=\"color: #444444;\">) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0currentCount = <\/span><span style=\"color: #0086b3;\">0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">for<\/span> <span class=\"pl-smi1\">elem<\/span> <span style=\"color: #a71d5d;\">:=<\/span> <span style=\"color: #a71d5d;\">range<\/span><span style=\"color: #444444;\"> countingChannel {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0currentCount++<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p>In the main function, we are creating a channel and calling <code> <span style=\"color: #444444;\">keepCount<\/span><\/code><span style=\"color: #444444;\"> as a <\/span><code><span style=\"color: #444444;\"> goroutine<\/span><\/code>. In <code><span style=\"color: #444444;\">keepCount<\/span><\/code> we simply listen on the channel in a loop and when we get a message we <span style=\"mso-spacerun: yes;\">\u00a0<\/span>increment <b style=\"font: 400 0.9em Consolas,'Courier New',Courier,monospace;\">currentCount<\/b>. We now need to get the <code><span style=\"color: #444444;\">handleLunchTimeRequest<\/span><\/code>function to call the <b style=\"font: 400 0.9em Consolas,'Courier New',Courier,monospace;\">countingChannel<\/b>. The <code><span style=\"color: #444444;\">handleLunchTimeRequest<\/span><\/code> function needs to have a specific signature so that it is called when the <code><span style=\"color: #444444;\">\/isitlunchtimeyet<\/span><\/code> URL is called; so we can&#8217;t just add the channel as an additional parameter but what we can do is to convert the function into a method. In go, a function is similar to a static method in c# <span style=\"mso-spacerun: yes;\">\u00a0<\/span>in that it doesn&#8217;t store any state itself, whereas a method is a function that has what is called a receiver, a type that has state. If we look at this example of a type and a method we can see that we can use the receiver to store objects:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">package<\/span><span style=\"color: #444444;\"> main<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">import<\/span><span style=\"color: #444444;\"> (<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">fmt<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">net\/http<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">time<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">type<\/span><span style=\"color: #444444;\"> httpHandler <\/span><span style=\"color: #a71d5d;\">struct<\/span><span style=\"color: #444444;\"> {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0countingChannel <\/span><span style=\"color: #a71d5d;\">chan<\/span> <span style=\"color: #a71d5d;\">int<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">main<\/span><span style=\"color: #444444;\">() {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">var<\/span> <span class=\"pl-smi1\">handler<\/span><span style=\"color: #444444;\"> httpHandler<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0handler.<\/span><span class=\"pl-smi1\">countingChannel<\/span><span style=\"color: #444444;\"> = <\/span><span style=\"color: #0086b3;\">make<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #a71d5d;\">chan<\/span> <span style=\"color: #a71d5d;\">int<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">go<\/span> <span style=\"color: #0086b3;\">keepCount<\/span><span style=\"color: #444444;\">(handler.<\/span><span class=\"pl-smi1\">countingChannel<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0http.<\/span><span style=\"color: #0086b3;\">HandleFunc<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">\/isitlunchtimeyet<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, handler.<\/span><span class=\"pl-smi1\">handleLunchTimeRequest<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0http.<\/span><span style=\"color: #0086b3;\">ListenAndServe<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">:8080<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, <\/span><span style=\"color: #0086b3;\">nil<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">keepCount<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">countingChannel<\/span> <span class=\"pl-v2\">chan<\/span> <span class=\"pl-v2\">int<\/span><span style=\"color: #444444;\">) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">currentCount<\/span> <span style=\"color: #a71d5d;\">:=<\/span> <span style=\"color: #0086b3;\">0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">for<\/span><span style=\"color: #444444;\"> _ = <\/span><span style=\"color: #a71d5d;\">range<\/span><span style=\"color: #444444;\"> countingChannel {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0currentCount++<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(currentCount)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">(<\/span><span class=\"pl-v2\">handler<\/span> <span class=\"pl-v2\">httpHandler<\/span><span style=\"color: #795da3;\">) handleLunchTimeRequest<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">res<\/span> <span class=\"pl-v2\">http<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">ResponseWriter<\/span><span style=\"color: #444444;\">, <\/span><span class=\"pl-v2\">req<\/span><span style=\"color: #444444;\"> *<\/span><span class=\"pl-v2\">http<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">Request<\/span><span style=\"color: #444444;\">) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">now<\/span> <span style=\"color: #a71d5d;\">:=<\/span><span style=\"color: #444444;\"> time.<\/span><span style=\"color: #0086b3;\">Now<\/span><span style=\"color: #444444;\">()<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0handler.<\/span><span class=\"pl-smi1\">countingChannel<\/span> <span style=\"color: #a71d5d;\">&lt;-<\/span> <span style=\"color: #0086b3;\">1<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">if<\/span><span style=\"color: #444444;\"> now.<\/span><span style=\"color: #0086b3;\">Hour<\/span><span style=\"color: #444444;\">() &gt; <\/span><span style=\"color: #0086b3;\">11<\/span><span style=\"color: #444444;\"> &amp;&amp; now.<\/span><span style=\"color: #0086b3;\">Hour<\/span><span style=\"color: #444444;\">() &lt; <\/span><span style=\"color: #0086b3;\">14<\/span><span style=\"color: #444444;\"> {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Fprintln<\/span><span style=\"color: #444444;\">(res, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">yes<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">return<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Fprintln<\/span><span style=\"color: #444444;\">(res, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">no<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/play.golang.org\/p\/n4aA06mQOX\"> https:\/\/play.golang.org\/p\/n4aA06mQOX<\/a><\/p>\n<p>There are a few new things to point out here, the first is that we have created our own type:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">type<\/span><span style=\"color: #444444;\"> httpHandler <\/span><span style=\"color: #a71d5d;\">struct<\/span><span style=\"color: #444444;\"> {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0countingChannel <\/span><span style=\"color: #a71d5d;\">chan<\/span> <span style=\"color: #a71d5d;\">int<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p>There is a single field or property, &#8220;<code><span style=\"color: #444444;\">countingChannel<\/span><\/code>&#8221; which is a channel that takes an int as a paramter. We then create a new instance of the <code><span style=\"color: #444444;\"> httpHandler<\/span><\/code> type with:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">var<\/span> <span class=\"pl-smi1\">handler<\/span><span style=\"color: #444444;\"> httpHandler<\/span><\/p>\n<\/div>\n<p>This creates a new <code><span style=\"color: #444444;\"> httpHandler<\/span><\/code>: By default, everything in go is initialized to zero; so we then need to set the <code><span style=\"color: #444444;\">countingChannel<\/span><\/code> to be a new channel:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">handler.<\/span><span class=\"pl-smi1\">countingChannel<\/span><span style=\"color: #444444;\"> = <\/span><span style=\"color: #0086b3;\">make<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #a71d5d;\">chan<\/span> <span style=\"color: #a71d5d;\">int<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<\/div>\n<p>Now, wherever we referenced the <code> <span style=\"color: #444444;\">countingChannel,<\/span><\/code> we change the references to the field on the handler object:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">go<\/span> <span style=\"color: #0086b3;\">keepCount<\/span><span style=\"color: #444444;\">(handler.<\/span><span class=\"pl-smi1\">countingChannel<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<\/div>\n<p>We then change <code><span style=\"color: #444444;\">http.HandleFunc<\/span><\/code> to call the method on our new handler instance:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">http.<\/span><span style=\"color: #0086b3;\">HandleFunc<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">\/isitlunchtimeyet<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, handler.<\/span><span class=\"pl-smi1\">handleLunchTimeRequest<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<\/div>\n<p>Our <code><span style=\"color: #444444;\">handleLunchTimeRequest<\/span><\/code> function is then converted into a method by preceding the name with the receiver name and type: Think of a receiver as a <b style=\"font: 400 0.9em Consolas,'Courier New',Courier,monospace;\"> this<\/b> pointer:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">(<\/span><span class=\"pl-v2\">handler<\/span> <span class=\"pl-v2\">httpHandler<\/span><span style=\"color: #795da3;\">) handleLunchTimeRequest<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">res<\/span> <span class=\"pl-v2\">http<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">ResponseWriter<\/span><span style=\"color: #444444;\">, <\/span><span class=\"pl-v2\">req<\/span><span style=\"color: #444444;\"> *<\/span><span class=\"pl-v2\">http<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">Request<\/span><span style=\"color: #444444;\">) {<\/span><\/p>\n<\/div>\n<p>We can then use the handler in the method to retrieve the correct instance of the channel:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0handler.<\/span><span class=\"pl-smi1\">countingChannel<\/span> <span style=\"color: #a71d5d;\">&lt;-<\/span> <span style=\"color: #0086b3;\">1<\/span><\/p>\n<\/div>\n<p>Now when you call the URL, you should see the counter being incremented. The important thing here is that we have used no locks or synchronization methods, no <code><span style=\"color: #444444;\">ReaderWriterLockSlim<\/span><\/code>&#8216;s but we guarantee that we can serve requests concurrently and we:<\/p>\n<ol style=\"list-style: lower-alpha;\">\n<li>do not block the handler goroutine<\/li>\n<li>do not miss any counts<\/li>\n<\/ol>\n<p>If we launched our version of <code> <span style=\"color: #444444;\">isitlunchtimeyet<\/span><\/code> we would undoubtable be swamped with requests for additional features, and the top one would likely be to support different meal types so we would want to allow the users to pass in the URL both the meal type. To do that, I would be inclined to make use of a different http router than the default, and one of the most popular ones is gorilla. The system it has of allowing parameters to be included in URLs is really great:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">package<\/span><span style=\"color: #444444;\"> main<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">import<\/span><span style=\"color: #444444;\"> (<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">fmt<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">net\/http<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">time<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">strings<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">github.com\/gorilla\/mux<\/span><span style=\"color: #df5000;\">&#8220;<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">type<\/span><span style=\"color: #444444;\"> httpHandler <\/span><span style=\"color: #a71d5d;\">struct<\/span><span style=\"color: #444444;\"> {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0countingChannel <\/span><span style=\"color: #a71d5d;\">chan<\/span> <span style=\"color: #a71d5d;\">int<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">main<\/span><span style=\"color: #444444;\">() {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">var<\/span> <span class=\"pl-smi1\">handler<\/span><span style=\"color: #444444;\"> httpHandler<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0handler.<\/span><span class=\"pl-smi1\">countingChannel<\/span><span style=\"color: #444444;\"> = <\/span><span style=\"color: #0086b3;\">make<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #a71d5d;\">chan<\/span> <span style=\"color: #a71d5d;\">int<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">go<\/span> <span style=\"color: #0086b3;\">keepCount<\/span><span style=\"color: #444444;\">(handler.<\/span><span class=\"pl-smi1\">countingChannel<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">router<\/span> <span style=\"color: #a71d5d;\">:=<\/span><span style=\"color: #444444;\"> mux.<\/span><span style=\"color: #0086b3;\">NewRouter<\/span><span style=\"color: #444444;\">()<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0router.<\/span><span style=\"color: #0086b3;\">HandleFunc<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">\/isit{meal}timeyet\/<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, handler.<\/span><span class=\"pl-smi1\">handleMealTimeRequest<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0http.<\/span><span style=\"color: #0086b3;\">ListenAndServe<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">:8080<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, router)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">keepCount<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">countingChannel<\/span> <span class=\"pl-v2\">chan<\/span> <span class=\"pl-v2\">int<\/span><span style=\"color: #444444;\">) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">currentCount<\/span> <span style=\"color: #a71d5d;\">:=<\/span> <span style=\"color: #0086b3;\">0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">for<\/span><span style=\"color: #444444;\"> _ = <\/span><span style=\"color: #a71d5d;\">range<\/span><span style=\"color: #444444;\"> countingChannel {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0currentCount++<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Println<\/span><span style=\"color: #444444;\">(currentCount)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">(<\/span><span class=\"pl-v2\">handler<\/span> <span class=\"pl-v2\">httpHandler<\/span><span style=\"color: #795da3;\">) handleMealTimeRequest<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">res<\/span> <span class=\"pl-v2\">http<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">ResponseWriter<\/span><span style=\"color: #444444;\">, <\/span><span class=\"pl-v2\">req<\/span><span style=\"color: #444444;\"> *<\/span><span class=\"pl-v2\">http<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">Request<\/span><span style=\"color: #444444;\">) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">vars<\/span> <span style=\"color: #a71d5d;\">:=<\/span><span style=\"color: #444444;\"> mux.<\/span><span style=\"color: #0086b3;\">Vars<\/span><span style=\"color: #444444;\">(req)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">meal<\/span> <span style=\"color: #a71d5d;\">:=<\/span><span style=\"color: #444444;\"> vars[<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">meal<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">]<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">now<\/span> <span style=\"color: #a71d5d;\">:=<\/span><span style=\"color: #444444;\"> time.<\/span><span style=\"color: #0086b3;\">Now<\/span><span style=\"color: #444444;\">()<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0handler.<\/span><span class=\"pl-smi1\">countingChannel<\/span> <span style=\"color: #a71d5d;\">&lt;-<\/span> <span style=\"color: #0086b3;\">1<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">switch<\/span><span style=\"color: #444444;\"> strings.<\/span><span style=\"color: #0086b3;\">ToLower<\/span><span style=\"color: #444444;\">(meal) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">case<\/span> <span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">lunch<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">:<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">if<\/span> <span style=\"color: #0086b3;\">isItLunchTimeYet<\/span><span style=\"color: #444444;\">(now) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Fprintln<\/span><span style=\"color: #444444;\">(res, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">yes<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">return<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">case<\/span> <span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">breakfast<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">:<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">if<\/span> <span style=\"color: #0086b3;\">isItBreakfastTimeYet<\/span><span style=\"color: #444444;\">(now) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Fprintln<\/span><span style=\"color: #444444;\">(res, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">yes<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">return<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">case<\/span> <span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">dinner<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">:<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">if<\/span> <span style=\"color: #0086b3;\">isItDinnerTimeYet<\/span><span style=\"color: #444444;\">(now) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Fprintln<\/span><span style=\"color: #444444;\">(res, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">yes<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">return<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">default<\/span><span style=\"color: #444444;\">:<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Fprintf<\/span><span style=\"color: #444444;\">(res, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">What is <\/span><span style=\"color: #0086b3;\">%v<\/span><span style=\"color: #a71d5d;\">?<\/span><span class=\"pl-cce2\"><span style=\"color: #a71d5d;\">\\n<\/span><\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, meal)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0res.<\/span><span style=\"color: #0086b3;\">WriteHeader<\/span><span style=\"color: #444444;\">(http.<\/span><span class=\"pl-smi1\">StatusTeapot<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">return<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fmt.<\/span><span style=\"color: #0086b3;\">Fprintln<\/span><span style=\"color: #444444;\">(res, <\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">no<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">isItLunchTimeYet<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">t<\/span> <span class=\"pl-v2\">time<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">Time<\/span><span style=\"color: #444444;\">) <\/span><span class=\"pl-v2\">bool<\/span><span style=\"color: #444444;\"> {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">return<\/span><span style=\"color: #444444;\"> t.<\/span><span style=\"color: #0086b3;\">Hour<\/span><span style=\"color: #444444;\">() &gt; <\/span><span style=\"color: #0086b3;\">11<\/span><span style=\"color: #444444;\"> &amp;&amp; t.<\/span><span style=\"color: #0086b3;\">Hour<\/span><span style=\"color: #444444;\">() &lt; <\/span><span style=\"color: #0086b3;\">14<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">isItBreakfastTimeYet<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">t<\/span> <span class=\"pl-v2\">time<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">Time<\/span><span style=\"color: #444444;\">) <\/span><span class=\"pl-v2\">bool<\/span><span style=\"color: #444444;\"> {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">return<\/span><span style=\"color: #444444;\"> t.<\/span><span style=\"color: #0086b3;\">Hour<\/span><span style=\"color: #444444;\">() &gt;= <\/span><span style=\"color: #0086b3;\">6<\/span><span style=\"color: #444444;\"> &amp;&amp; t.<\/span><span style=\"color: #0086b3;\">Hour<\/span><span style=\"color: #444444;\">() &lt; <\/span><span style=\"color: #0086b3;\">10<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">isItDinnerTimeYet<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">t<\/span> <span class=\"pl-v2\">time<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">Time<\/span><span style=\"color: #444444;\">) <\/span><span class=\"pl-v2\">bool<\/span><span style=\"color: #444444;\"> {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a71d5d;\">return<\/span><span style=\"color: #444444;\"> t.<\/span><span style=\"color: #0086b3;\">Hour<\/span><span style=\"color: #444444;\">() &gt; <\/span><span style=\"color: #0086b3;\">16<\/span><span style=\"color: #444444;\"> &amp;&amp; t.<\/span><span style=\"color: #0086b3;\">Hour<\/span><span style=\"color: #444444;\">() &lt; <\/span><span style=\"color: #0086b3;\">20<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">}<\/span><\/p>\n<\/div>\n<p>The interesting parts of this are that we now use the gorilla routing instead of the default routing:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">router<\/span> <span style=\"color: #a71d5d;\">:=<\/span><span style=\"color: #444444;\"> mux.<\/span><span style=\"color: #0086b3;\">NewRouter<\/span><span style=\"color: #444444;\">()<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0router.<\/span><span style=\"color: #0086b3;\">HandleFunc<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">\/isit{meal}timeyet\/<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, handler.<\/span><span class=\"pl-smi1\">handleMealTimeRequest<\/span><span style=\"color: #444444;\">) <\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0http.<\/span><span style=\"color: #0086b3;\">ListenAndServe<\/span><span style=\"color: #444444;\">(<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">:8080<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">, router)<\/span><\/p>\n<\/div>\n<p>This means that we are looking for two URLs, the first is &#8220;\/isit&#8221; then any word and then &#8220;timeyet&#8221; &#8211; this lets us accept lunch, breakfast or anything &#8211; we need to be aware of that when we read the variable from the URL:<\/p>\n<div style=\"border: #cacaca 1px solid; text-align: left; margin-left: 20px; padding: 10px 3px 3px 10px; ;font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\">\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #a71d5d;\">func<\/span> <span style=\"color: #795da3;\">(<\/span><span class=\"pl-v2\">handler<\/span> <span class=\"pl-v2\">httpHandler<\/span><span style=\"color: #795da3;\">) handleMealTimeRequest<\/span><span style=\"color: #444444;\">(<\/span><span class=\"pl-v2\">res<\/span> <span class=\"pl-v2\">http<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">ResponseWriter<\/span><span style=\"color: #444444;\">, <\/span><span class=\"pl-v2\">req<\/span><span style=\"color: #444444;\"> *<\/span><span class=\"pl-v2\">http<\/span><span style=\"color: #444444;\">.<\/span><span class=\"pl-v2\">Request<\/span><span style=\"color: #444444;\">) {<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">vars<\/span> <span style=\"color: #a71d5d;\">:=<\/span><span style=\"color: #444444;\"> mux.<\/span><span style=\"color: #0086b3;\">Vars<\/span><span style=\"color: #444444;\">(req)<\/span><\/p>\n<p style=\"margin: 0; font: 400 0.9em Consolas,'Courier New',Courier,monospace; background: #f9f9f9;\"><span style=\"color: #444444;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span class=\"pl-smi1\">meal<\/span> <span style=\"color: #a71d5d;\">:=<\/span><span style=\"color: #444444;\"> vars[<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #a71d5d;\">meal<\/span><span style=\"color: #df5000;\">&#8220;<\/span><span style=\"color: #444444;\">]<\/span><\/p>\n<\/div>\n<p>&#8220;meal&#8221; is the word that is between &#8220;\/isit&#8221; and &#8220;timeyet&#8221; &#8211; we then simply have a switch to let us choose which set of timings to ask for. If we don&#8217;t understand what the meal word was then we will fall to the default case and can return an appropriate error message.<\/p>\n<h3>Conclusion<\/h3>\n<p>I think that you can pretty easily build up an API or web service to do something useful, there are packages that let you connect to SQL Server and pretty much every other data source that a c# developer could be used to already. Go isn&#8217;t perfect for everything; HTML templating for example isn&#8217;t great, but a good approach to building web sites is to use go to run the API layer and drive the the front end with node.js or something similar: And if simplicity and scale is what you&#8217;re after, <span style=\"mso-spacerun: yes;\">\u00a0<\/span>go is a language worth investigating.<\/p>\n<h1>References<\/h1>\n<ol style=\"list-style: none;\">\n<li style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/golang.org\/\"> https:\/\/golang.org\/<\/a><\/li>\n<li style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/github.com\/golang\/go\/wiki\"> https:\/\/github.com\/golang\/go\/wiki<\/a><\/li>\n<li style=\"margin-bottom: .0001pt;\"><a href=\"https:\/\/blog.golang.org\/\"> https:\/\/blog.golang.org\/<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>When Google announced the &#8216;Go&#8217; language in 2009 we were all underexcited. After all, a  compiled, statically typed language in the tradition of Algol and C isn&#8217;t that radical, especially one that eschewed generic programming, implicit type conversions, assertions, inheritance and pointer arithmetic. However, it has proved to be robust, highly-portable, simple to use, and productive to work with. Ed Elliott reckons that it is definitely worth checking it out. &hellip;<\/p>\n","protected":false},"author":59464,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143538],"tags":[],"coauthors":[11314],"class_list":["post-70014","post","type-post","status-publish","format-standard","hentry","category-dotnet-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/70014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/59464"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=70014"}],"version-history":[{"count":8,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/70014\/revisions"}],"predecessor-version":[{"id":72801,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/70014\/revisions\/72801"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=70014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=70014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=70014"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=70014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}