{"id":18428887,"url":"https://github.com/akshaybabloo/go-notes","last_synced_at":"2025-04-13T21:17:28.163Z","repository":{"id":70617278,"uuid":"89419474","full_name":"akshaybabloo/Go-Notes","owner":"akshaybabloo","description":"Go programming language notes","archived":false,"fork":false,"pushed_at":"2019-06-30T03:07:27.000Z","size":498,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T21:17:25.123Z","etag":null,"topics":["go","tutorial"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/akshaybabloo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-26T00:30:19.000Z","updated_at":"2025-01-30T03:31:24.000Z","dependencies_parsed_at":"2023-04-15T06:50:33.357Z","dependency_job_id":null,"html_url":"https://github.com/akshaybabloo/Go-Notes","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akshaybabloo%2FGo-Notes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akshaybabloo%2FGo-Notes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akshaybabloo%2FGo-Notes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akshaybabloo%2FGo-Notes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/akshaybabloo","download_url":"https://codeload.github.com/akshaybabloo/Go-Notes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248782259,"owners_count":21160717,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","tutorial"],"created_at":"2024-11-06T05:14:56.025Z","updated_at":"2025-04-13T21:17:28.140Z","avatar_url":"https://github.com/akshaybabloo.png","language":"Go","readme":"# Introduction to Go\n\n\u003e Note 1: The code in this repository is based on Go 1.11.*. It should work on other versions as well.\n\n\u003e Note 2: The file names do not follow the Go language convention, it's there only to show you the flow of this tutorial.\n\n\u003c!-- TOC --\u003e\n\n- [1. Installation](#1-installation)\n  - [1.1. Windows](#11-windows)\n  - [1.2. macOS](#12-macos)\n  - [1.3. Linux](#13-linux)\n- [2. Setup](#2-setup)\n  - [2.1. Setting up IDE](#21-setting-up-ide)\n- [3. Getting Stated and Conventions](#3-getting-stated-and-conventions)\n  - [3.1. Using the Playground](#31-using-the-playground)\n  - [3.2. Coding and Naming Conventions](#32-coding-and-naming-conventions)\n    - [3.2.1. Naming Conventions](#321-naming-conventions)\n      - [3.2.1.1. File Names](#3211-file-names)\n      - [3.2.1.2. Function Names](#3212-function-names)\n  - [3.3. Go Command Line](#33-go-command-line)\n    - [3.3.1. go](#331-go)\n    - [3.3.2. godoc](#332-godoc)\n    - [3.3.3. gofmt](#333-gofmt)\n  - [3.4. Workspace](#34-workspace)\n- [4. Let's Start Coding](#4-lets-start-coding)\n  - [4.1. Go Essential](#41-go-essential)\n    - [4.1.1. Hello World](#411-hello-world)\n    - [4.1.2. String Formatting](#412-string-formatting)\n    - [4.1.3. Getting Input from Console](#413-getting-input-from-console)\n  - [4.2. Managing Values](#42-managing-values)\n    - [4.2.1. Exploring variables, constants and types](#421-exploring-variables-constants-and-types)\n      - [4.2.1.1. Implicit vs Explicit](#4211-implicit-vs-explicit)\n      - [4.2.1.2. Constants](#4212-constants)\n      - [4.2.1.3. Data Types](#4213-data-types)\n    - [4.2.2. Working with Strings](#422-working-with-strings)\n    - [4.2.3. Using Math Operator and Math Package](#423-using-math-operator-and-math-package)\n    - [4.2.4. Date and Time](#424-date-and-time)\n  - [4.3. Managing Complex Types and collections](#43-managing-complex-types-and-collections)\n    - [4.3.1. Pointers](#431-pointers)\n    - [4.3.2. Arrays](#432-arrays)\n    - [4.3.3. Slices](#433-slices)\n    - [4.3.4. Memory](#434-memory)\n      - [4.3.4.1. The `new()` Function](#4341-the-new-function)\n      - [4.3.4.2. The `make()` Function](#4342-the-make-function)\n      - [4.3.4.3. Creating `nil` Objects](#4343-creating-nil-objects)\n      - [4.3.4.4. Memory Deallocation](#4344-memory-deallocation)\n    - [4.3.5. Maps](#435-maps)\n    - [4.3.6. Data Structure](#436-data-structure)\n  - [4.4. Managing Program Flow](#44-managing-program-flow)\n    - [4.4.1. Conditional Logic](#441-conditional-logic)\n    - [4.4.2. Switch Statement](#442-switch-statement)\n    - [4.4.3. Loops](#443-loops)\n  - [4.5. Structuring Go Code](#45-structuring-go-code)\n    - [4.5.1. Defining and Calling a Function](#451-defining-and-calling-a-function)\n    - [4.5.2. Multiple Return Value for a Function](#452-multiple-return-value-for-a-function)\n    - [4.5.3. Creating Packages as Function Libraries](#453-creating-packages-as-function-libraries)\n    - [4.5.4. Methods](#454-methods)\n    - [4.5.5. Interfaces](#455-interfaces)\n    - [4.5.6. Handling Errors](#456-handling-errors)\n    - [4.5.7. Defer Function Call](#457-defer-function-call)\n  - [4.6. Working with Files and web](#46-working-with-files-and-web)\n    - [4.6.1. Writing to a Text File](#461-writing-to-a-text-file)\n    - [4.6.2. Reading from a Text File](#462-reading-from-a-text-file)\n    - [4.6.3. Walking Directory Tree](#463-walking-directory-tree)\n    - [4.6.4. HTTP File Read](#464-http-file-read)\n    - [4.6.5. Read and Write JSON Data](#465-read-and-write-json-data)\n    - [4.6.6. Simple HTTP Server](#466-simple-http-server)\n- [5. What's Next](#5-whats-next)\n\n\u003c!-- /TOC --\u003e\n\n## 1. Installation\n\nInstallation is straightforward, just download the executable file and follow the instruction.\n\n### 1.1. Windows\n\nFor windows, you will have to download the MSI executable file. Remember that MSI is a 64-bit so make sure you have a 64-bit processor.\n\nGoto [https://golang.org/dl/](https://golang.org/dl/) and download the latest version, for example for Go 1.10.1 it is `go1.10.1.windows-amd64.msi`. Once installed, you check it by opening your command prompt and type in `go version`.\n\n### 1.2. macOS\n\nA pre-built package is available to download and install. Just follow the installation window. Download the latest version at [https://golang.org/dl/](https://golang.org/dl/), for example, for Go 1.10.1 the file name is `go1.10.1.darwin-amd64.pkg`\n\n### 1.3. Linux\n\nFor Linux distribution, there is no pre-built package, but it is easy to install. Just download the latest *.tar.gz file from [https://golang.org/dl/](https://golang.org/dl/), for example, for Go 1.10.1 the file is `go1.10.1.linux-amd64.tar.gz`.\n\nOnce downloaded, follow the steps:\n\n1. Open your terminal and change the directory to where you download the tar.gz file, then type in:\n\n  ```bash\n  tar -c /usr/local -xzf go1.10.1.linux-amd64.tar.gz\n  ```\n\n2. Then add the `/usr/local/go/bin` to the path by doing:\n\n```bash\nexport PATH=$PATH:/usr/local/go/bin\n```\n\nYou can test it by typing in `go version`.\n\n\u003e Note: You can also add file to your home file by just replacing the `/usr/local/` with `/home/username`, `username` being your user folder.\n\n## 2. Setup\n\nWindows and macOS should automatically update the path to Go, if not you will have to add the path - `installed-folder/go/bin`, where installed-path is where you have installed the Go language. For Linux, see above.\n\n### 2.1. Setting up IDE\n\nI am using JetBrains GoLand IDE but you can use any IDE you wish, I would recommend using VSCode.\n\n## 3. Getting Stated and Conventions\n\n### 3.1. Using the Playground\n\nGo website has a wonderful way to test your code without installing Go libraries on your system, its called [The Go Playground](https://play.golang.org/).\n\n![The Go Playground](https://github.com/akshaybabloo/Go-Notes/raw/master/assets/images/playground.JPG)\n\nGo also provides a way to run playground (you will need internet though) on your system. Type in:\n\n```bash\n\u003e godoc -http=:6060 -play -index\n```\n\nNow open your browser and type in `localhost:6060`, this should open playground.\n\nThere are few limitations when using the Go playground as its being run on a secured server that has no access to external world:\n\n1. You cannot run HTTP requests or host your web server.\n2. The playground fakes the files system so that your IO works correctly.\n3. The date and time are always constant - November 10, 2009, at 11:00 p.m.\n4. You can work with only one source code at a time; you cannot have multiple go file and import them locally unlike you do on your system.\n5. Moreover, it is free.\n\n### 3.2. Coding and Naming Conventions\n\nBefore we dive into the coding part of Go language, let's look into some coding and naming conventions of this programming language.\n\n#### 3.2.1. Naming Conventions\n\n##### 3.2.1.1. File Names\n\nAccording to the Go docs, there is no right or wrong way to name your file - you can either use underscores or camelCase or Pascal Case or small letters, but most Go developers use underscore to name their files. The underscore contradicts the standard library files, for example:\n\n```\ngo/src/mime/encodedword.go\ngo/src/mime/encodedword_test.go\n```\n\n`encodedword.go` is the name of the main file where other methods are available, and their tests can be found in `encodedword_test.go`. Simple as.\n\nSometimes you wish to run the file on Windows, Linux or both. In that case, you can name your files something like this\n\n```\nfilename.go - compiles universally\nfilename_windows.go - compiles on windows only\nfilename_unix.go - compiles on posix.\nfilename_amd64.go - compiles on 64-bit processors only\nfilename_arm.go - compiles on arm processors only\n```\n\nGo build takes care of what to use depending on your operating system, for more information see [https://golang.org/pkg/go/build/](https://golang.org/pkg/go/build/)\n\n##### 3.2.1.2. Function Names\n\nA function always starts with the keyword `func`, the name given to it are of two types; one that begins with a capital letter and the other that begins with a small letter.\n\nFor example:\n\n```go\npackage main\n\nimport (\n  \"strconv\"\n  \"fmt\"\n  \"reflect\"\n)\n\nfunc main() {\n  sum := Add(1, 2)\n\n  fmt.Println(sum)\n  fmt.Println(reflect.TypeOf(sum))\n}\n\nfunc Add(a int, b int) string {\n  return toString(a + b)\n}\n\nfunc toString(c int) string {\n  return strconv.Itoa(c)\n}\n```\n\nLet's consider the above example, the `Add()` function takes in two numbers, adds it and converts it to a string by using `toString()` function. The `Add()` function is available throughout the application, as in it can be imported to other go files. Whereas the `toString()` can only be used locally.\n\n### 3.3. Go Command Line\n\nUnder `c:\\go\\bin` there are three commands:\n\n![Go CLI](https://github.com/akshaybabloo/Go-Notes/raw/master/assets/images/go_cli.JPG)\n\n#### 3.3.1. go\n\nThis is the primary command you will be using throughout the tutorial. It incorporates more than a dozen commands in it. We will look into most of them in this tutorial. If you type in `go` you will see all the available commands and its description.\n\n#### 3.3.2. godoc\n\n`godoc` is similar to `javadoc` but for Go packages. If you enter `godoc fmt`, it will display all the documentation related to `fmt` package. Typing `godoc` itself will print out all the command flags it can take.\n\n#### 3.3.3. gofmt\n\nThis command can be used to format your source code. You can use this by typing `gofmt filename.go` this will only print out the formatted text from the file but does not write it, to make it write the file you should add `-w` flag - `gofmt -w filename.go`.\n\n### 3.4. Workspace\n\nGo follows some strict norms on how you create your workspace structure. The root directory can be any name but should not contain any space or special characters.\n\nThe structure should look something like this:\n\n```\nworkspacename\n|\n+- src\n+- bin\n`- pkg\n```\n\n`src` is for your source files, `bin` for executables and `pkg` for external libraries.\n\nOnce, the folder has been set; the next thing is to create a `GOPATH` to do so type in:\n\n```\nset GOTPATH=%USERPROFILE%\\gowork\n```\n\n\u003e Note: I would create this directory in my `home` folder. It is up to you where to create it.\n\nYou can check all the Go environments by typing in `go env`. When you change directory to the `src` folder with a go file in it and type in `go install` it creates an executable file in the `bin` folder.\n\n## 4. Let's Start Coding\n\n\u003e Note: Ignore `_#` (where `#` is the number) as the filename below, the number represents the flow of things to start and end with.\n\n**Code Structure**\n\n```\nsrc\n|\n`- helloworld_1.go\n```\n\n### 4.1. Go Essential\n\nSome helpful Go CLI commands:\n\n1. `godoc` - Used to get the documentation for a given package. Example `godoc fmt`.\n2. `gofmt` - To format a Go file to its specifications. Example `gofmt -w filename.go` `-w` will write the file.\n3. `go` - It has a lot of sub-commands, in this notes we will be using mostly `go run`\n  1. `go run` - Builds and runs a Go file. Example `go run filename.go`\n  2. `go build` - Build an executable file. Example `go build filename.go`\n\n#### 4.1.1. Hello World\n\n[Source - helloworld_1.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/helloworld_1.go)\n\nSay we have a file called `helloworld_1.go`, which has the following content.\n\n```go\npackage main // The main method should always be in \"main\" package\n\nimport \"fmt\" // Standard Go library for formatted I/O\n\nfunc main() { // Go always starts at \"main\" method\n  fmt.Println(\"hello world\") // outputs to I/O with new line.\n}\n```\n\nTo run the file you should use - `go build filename.go` this compiles the file to `filename.exe` (in windows). In this case it will look something like this:\n\n![Go build helloworld_1.go](https://github.com/akshaybabloo/Go-Notes/raw/master/assets/images/go_build.JPG)\n\nOr you can also use `go run` to build the file in `temp` folder and run it. Something like:\n\n![Go build helloworld_1.go](https://github.com/akshaybabloo/Go-Notes/raw/master/assets/images/go_run.JPG)\n\n#### 4.1.2. String Formatting\n\n[Source - stringformats_2.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/stringformats_2.go)\n\nThis section looks into [pkg](https://golang.org/pkg/fmt/) package.\n\nA function in Go can return `n` number of objects, which includes `errors`.\n\nFor example `fmt.Println` can have multiple returns:\n\n```go\nstringLength, err := fmt.Println(str1, str2)\n```\n\nIf there is no error, the content in the `stringLength` will be returned and the `err` will be equal to `nil`.\n\n#### 4.1.3. Getting Input from Console\n\n[Source - consoleinput_3.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/consoleinput_3.go)\n\nFor user input you can use `fmt.Scanln(\u0026ref)` and reference to a string variable, but the problem with the `Scanln` is that whenever it encounters a space, the word after the space is given to a new variable.\n\nfor example:\n\n```go\nvar s string\nvar s2 string\n\n// Send in the reference of the string s\nfmt.Scanln(\u0026s, \u0026s2)  // hello hi\n// Print the value of it.\nfmt.Println(s, s2)  // hello hi\n```\n\nTo take the input from the user as is, you will have to use different packages, such as `bufio` and `os`.\n\n### 4.2. Managing Values\n\nIn this section we will look into formatting string, date time, date types.\n\n#### 4.2.1. Exploring variables, constants and types\n\n##### 4.2.1.1. Implicit vs Explicit\n\nUsing `var` keyword and an assignment operator with a data type you can declare a value.\n\n```go\nvar anInteger int = 30\nvar aString string = \"Hello!\"\n```\n\nGo allows you to implicitly declare a variable without using a data type and the `var` keyword and replace them by using `:=`.\n\n```go\nanInteger := 30\naString := \"Hello!\"\n```\n\nAbove code is still static and cannot be changed.\n\n##### 4.2.1.2. Constants\n\nExplicit:\n\n```go\nconst anInteger int = 30\nconst aString string = \"Hello!\"\n```\n\nImplicit:\n\n```go\nconst anInteger = 30\nconst aString = \"Hello!\"\n```\n\n##### 4.2.1.3. Data Types\n\nAll data types are spelled in lower case.\n\n1. `bool` - Booleans values\n2. `string` - String type\n3. Integers:\n  1. Fixed Integers:\n  ```\n  uint8 uint16 uint32 uint64\n  int8 int16 int32 int64\n  ```\n  2. Aliases: They could be a 32 bit or a 64 bit depending upon the operating system.\n  ```\n  byte uint int uintptr\n  ```\n4. Floating values:\n  ```\n  float32 float64\n  ```\n5. Complex numbers:\n  ```\n  complex64 complex128\n  ```\n6. Data collections:\n  ```\n  Arrays Slices Maps Structs\n  ```\n7. Language organization:\n  ```\n  Functions Interfaces Channels\n  ```\n8. Data Management:\n  ```\n  Pointers\n  ```\n\n#### 4.2.2. Working with Strings\n\n[Source - workingwithstrings_4.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/workingwithstrings_4.go)\n\nGo language is case sensitive as in `hello != Hello`. For more information on `strings` see [http://golang.com/pkg/strings/](https://golang.com/pkg/strings/)\n\n#### 4.2.3. Using Math Operator and Math Package\n\n[Source - math_5.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/math_5.go)\n\nMath Operation\n\n| Operator | Definition |\n|:--------:|:----------:|\n|     +    |     Add    |\n|     -    |  Subtract  |\n|     *    |  Multiply  |\n|     /    |   Divide   |\n|     %    |  Remainder |\n\nBitwise Operation\n\n| Operator |  Definition |\n|:--------:|:-----------:|\n|     \u0026    | Bitwise AND |\n|     \\|   |  Bitwise OR |\n|     ^    | Bitwise XOR |\n|    \u0026^    |   Bitclear  |\n|    \u003e\u003e    | Right Shift |\n|    \u003c\u003c    |  Left Shift |\n\nIn Go, numeric types don't implicitly convert, i.e you cannot add a float to int, the only way you can add ghem is by converting an int to float. For example:\n\n```go\nint1 := 30\nfloat1 :=30.1\nfmt.Println(float64(int1) + float1)\n```\n\nFor more complex math problems see [http://golang.com/pkg/math/](https://golang.com/pkg/math/).\n\n#### 4.2.4. Date and Time\n\n[Source - datetime_6.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/datetime_6.go)\n\nWhen trying to format date and time make sure you format based on the constant numbers given in the package.\n\n```\n1 - Month\n2 - Day\n3 - Hour\n4 - Minutes\n5 - Seconds\n6 - Year\n7 - Timezone\n```\n\nFor example:\n\n```go\nshortFormat := \"2/1/06\"\nfmt.Println(\"Today is\", time.Now().Format(shortFormat))\n```\n\n### 4.3. Managing Complex Types and collections\n\nIn this section we will look into some complex types such as pointers and collections such as arrays, maps and Structs.\n\n#### 4.3.1. Pointers\n\n[Source - pointers_7.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/pointers_7.go)\n\nLike C/C++, Go also support pointers natively.\n\n#### 4.3.2. Arrays\n\n[Source - arrays_8.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/arrays_8.go)\n\nAll the contents in an array are of same type. Like Java, the first index always starts with `0`.\n\nThere are some memory implication when using arrays in Go, unlike C/C++ (where arrays are pointers) arrays in Go are values i.e. when assigning one array to another copies all the contents of it. Passing an array to the function will pass a copy of the array and not the pointer.\n\n#### 4.3.3. Slices\n\n[Source - slices_9.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/slices_9.go)\n\nA slice is an abstraction layer that sits on top of an Array. Like arrays all the contents in the slices are of the same type, but unlike it, slices can are resizeable and can be sorted easily.\n\n#### 4.3.4. Memory\n\nGo's runtime is statistically linked into each compiled application. Go's memory is managed my the runtime on a different thread, that means memory allocation and deallocation is completely done automatically in the background.\n\nTo understand how the memory allocation works there two function that you'll need to understand - `make()` and `new()`, these function can be used to initialise `maps`, `slices` and `channels`.\n\nIn most cases you will like be using `make()` instead of `new()`.\n\n##### 4.3.4.1. The `new()` Function\n\n1. Allocates but does not initialise memory\n2. Results in zeroed storage but returns a memory address\n\n##### 4.3.4.2. The `make()` Function\n\n1. Allocates and initialise memory\n2. Results in non-zeroed storage but returns a memory address\n\n##### 4.3.4.3. Creating `nil` Objects\n\nYou must always initialise complex objects before adding values, declaring them without `make()` will cause a _panic_.\n\nFor example:\n\n```go\nvar m map[string]int // key value pair of string and integer.\nm[\"key\"] = 10\nfmt.Println(m)\n```\n\nWith the above code you will get a _panic_ error at the second line as you cannot add a value to zeroed memory. To fix this you can do;\n\n```go\nm := make(map[string]int) // key value pair of string and integer.\nm[\"key\"] = 10\nfmt.Println(m)\n// map[key:42]\n```\n\n##### 4.3.4.4. Memory Deallocation\n\nMemory is deallocated by using garbage collector (GC), only the objects that are out of scope or set to `nil` are eligible.\n\nFor more information on GC you can go to [https://golang.org/pkg/runtime/](https://golang.org/pkg/runtime/).\n\n#### 4.3.5. Maps\n\n[Source - maps_10.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/maps_10.go)\n\nMaps are unordered collections of key-value pairs aka hash table.\n\n#### 4.3.6. Data Structure\n\n[Source - structs_11.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/structs_11.go)\n\nStructs in Go have similar function to C's struct and Java's classes. They both contain data and methods, but unlike Java, Go doesn't have an inheritance model.\n\nIn this section we will look into how to use struct to store data.\n\n### 4.4. Managing Program Flow\n\n#### 4.4.1. Conditional Logic\n\n[Source - conditions_12.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/conditions_12.go)\n\nIn this section we will look at how the conditions in Go differs from C.\n\nYou will have to remember that the `else` keyword should come after the braces. Example:\n\n```go\n\n// This is wrong. you will get an error\nif ...{\n  ...\n}\nelse {\n  ...\n}\n\n// Instead use this\nif ... {\n  ...\n} else {\n  ...\n}\n```\n\nThis is because the way the Go lexer see the line feeds.\n\n#### 4.4.2. Switch Statement\n\n[Source - switch_13.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/switch_13.go)\n\nGo's switch statement improvises C's and Java's. Like the recent versions of Java, Go can evaluate simple types, not just constants and strings. The code flow jumps from one case to another once the case if found, this means you don't have to add the redundant `break` statement.\n\n#### 4.4.3. Loops\n\n[Source - loops_14.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/loops_14.go)\n\nUnlike Java there is no `while` statement in Go, but there are extended version of the `for` loop, which will be seen in this section.\n\n### 4.5. Structuring Go Code\n\n#### 4.5.1. Defining and Calling a Function\n\n[Source - functions_15.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/functions_15.go)\n\nGo is organised as packages, each of these packages has functions. Any function starting with a small letter is private to that package but if it's starting with a capital letter it's public and be used outside the package.\n\nUnlike Java, Go has no function overloading in Go that means you'll need to create a different function for each output you want.\n\n#### 4.5.2. Multiple Return Value for a Function\n\n[Source - multiple_return_values_16.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/multiple_return_values_16.go)\n\nFunctions in Go can return multiple values, its the second is usually an error vale (we will talk about this in handling errors).\n\n#### 4.5.3. Creating Packages as Function Libraries\n\n[Source - packages_17](https://github.com/akshaybabloo/Go-Notes/blob/master/src/packages_17)\n\nFor this thing to work, you will have to setup a [workspace](#34-workspace) as mentioned at the starting of the tutorial. Once you have this, create a file as `\u003cGOPATH\u003e/src/libraries/library.go` and copy the `main` from [multiple_return_values_16.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/multiple_return_values_16.go) to it. After this is done, next create another file under `\u003cGOPATH\u003e/src/stringutil/stringutil.go`, copy `FullName` and `FullNameNakedReturn` to it or create any public functions.\n\nOpen your terminal, change your directory to `\u003cGOPATH\u003e/src/libraries/library.go` and type in `go install`, this will create a executable file under `\u003cGOPATH\u003e/bin/libraries.exe`.\n\n#### 4.5.4. Methods\n\n[Source - methods_18.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/methods_18.go)\n\nUnlike Java or C++ where a method is a member of a `class`, in Go, a method is a member of `type`.\n\n#### 4.5.5. Interfaces\n\n[Source - interfaces_19.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/interfaces_19.go)\n\nUnlike Java, Go does not have an `extends` keyword, rather an method that has the same type and name is an interface.\n\n#### 4.5.6. Handling Errors\n\n[Source - errors_20.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/errors_20.go)\n\nUnlike other programming languages, Go does not provide classic exception handling like `try...catch..` block.\n\n#### 4.5.7. Defer Function Call\n\n[Source - defer_21.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/defer_21.go)\n\nDeferred statements are executed last in first out order.\n\n### 4.6. Working with Files and web\n\n#### 4.6.1. Writing to a Text File\n\n[Source - fileWrite_22.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/fileWrite_22.go)\n\n#### 4.6.2. Reading from a Text File\n\n[Source - fileRead_23.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/fileRead_23.go)\n\n#### 4.6.3. Walking Directory Tree\n\n[Source - walkingDirectory_24.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/walkingDirectory_24.go)\n\n#### 4.6.4. HTTP File Read\n\n[Source - httpFileRead_25.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/httpFileRead_25.go)\n\n#### 4.6.5. Read and Write JSON Data\n\n[Source - json_26.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/json_26.go)\n\n#### 4.6.6. Simple HTTP Server\n\n[Source - httpServer_27.go](https://github.com/akshaybabloo/Go-Notes/blob/master/src/httpServer_27.go)\n\n## 5. What's Next\n\nIn the next tutorial (coming soon) we will look into using Go to develop a RESTful web application.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakshaybabloo%2Fgo-notes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakshaybabloo%2Fgo-notes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakshaybabloo%2Fgo-notes/lists"}