{"id":19261984,"url":"https://github.com/hacksu/intro_to_go","last_synced_at":"2025-08-08T21:25:00.809Z","repository":{"id":149141460,"uuid":"215125828","full_name":"hacksu/intro_to_go","owner":"hacksu","description":"Learning the basics of Go with a basic shopping application","archived":false,"fork":false,"pushed_at":"2019-10-15T16:46:19.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-04-09T21:54:52.548Z","etag":null,"topics":["advanced","lesson"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hacksu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2019-10-14T19:14:19.000Z","updated_at":"2024-06-19T03:57:32.655Z","dependencies_parsed_at":null,"dependency_job_id":"f855de48-203a-47ba-bdf2-fded7122d65c","html_url":"https://github.com/hacksu/intro_to_go","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/hacksu%2Fintro_to_go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2Fintro_to_go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2Fintro_to_go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2Fintro_to_go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hacksu","download_url":"https://codeload.github.com/hacksu/intro_to_go/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240364295,"owners_count":19789756,"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":["advanced","lesson"],"created_at":"2024-11-09T19:29:17.890Z","updated_at":"2025-02-23T18:40:59.081Z","avatar_url":"https://github.com/hacksu.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# intro_to_go\n![Go](https://golang.org/lib/godoc/images/go-logo-blue.svg)\n\nThe aim of this lesson is to cover the basics of the Go programming language\n\nSome important things to know about Go:\n* It was designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson\n* Syntactically similar to C\n* It is not exactly an object oriented language\n    * No classes, but there are structures\n    * You can define methods for these structures\n    * Still similar to OOP\n* [How to write Go code](https://golang.org/doc/code.html)\n    * The `go` tool requires you to organize your code in a specific way\n    * We will cover the basics in this lesson, but use this for further reading or as a reference\n\n## Getting started\n\nFirst, head to [the official Go website](https://golang.org/) and download the latest version of Go\n\nI would recommend using VS Code with the Go extension for development. That is what we'll be using in this lesson.\n* We will compile and run Go in the command line\n* There is a Go IDE, GoLand by JetBrains, as well if you are interested (but we won't be really covering that)\n\n## Your first Go program\n\nOpen up the command line, and navigate to your Go workspace:\n* `C:\\Users\\YourName\\go\\src` on Windows\n* `$HOME/go/src` on Unix based systems\n* The location of your workspace can be changed. See \"How to write Go code\" above\n\nMake a new directory (folder) called `shopping`\n\nNavigate into `shopping` and create a file called `main.go`. We will write the following code\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n)\n\nfunc main() {\n\tfmt.Println(\"Wow I did it\")\n}\n```\n\nTo run this, type `go run main.go` into the command line\n\nCongrats, you've written your first Go program!\n\n## Getting some input\n\nAn important part of this application is reading in user input, so we'll learn about that next\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n)\n\nfunc main() {\n\tfmt.Println(\"What is your name?\")\n\n\tvar name string\n\tfmt.Scanln(\u0026name)\n\n\tfmt.Println(\"Hello,\", name)\n}\n```\n\nAgain, type `go run main.go` into the command line to run this\n\n## Adding some structures\n\nNow that basic I/O has been covered, let's move on to structures\n\nHere's a basic structure that we're going to be using:\n\n```go\ntype product struct {\n    name string\n    cost int\n}\n```\n\nNow we can create a new product:\n\n```go\nfunc main() {\n\tp := product{\n\t\tname: \"Brick\",\n\t\tcost: 10,\n\t}\n\n\tfmt.Println(p.name, \"costs $\", p.cost)\n}\n```\n\nUsing structs, we can begin to build a more complex application\n\n## Creating the store\n\nWe're going to start building our store application\n\n```go\nfunc main() {\n\tbrick := product{name: \"brick\", cost: 10}\n\tcorn := product{name: \"corn\", cost: 6}\n\tgamecube := product{name: \"gamecube\", cost: 100}\n\n\tinventory := []product{brick, corn, gamecube}\n\n\tfmt.Println(\"Welcome to the store!\")\n\tfmt.Println(\"Options:\")\n\tfmt.Println(\"(i)nventory\")\n\tfmt.Println(\"(b)uy item\")\n\tfmt.Println(\"(a)dd item\")\n\tfmt.Println(\"(q)uit\")\n\n\tvar choice string\n\tfmt.Scanln(\u0026choice)\n\n\tfor choice != \"q\" {\n\t\tswitch choice {\n\t\tcase \"i\":\n\t\t\tfor _, p := range inventory {\n\t\t\t\tfmt.Println(\"name:\", p.name)\n\t\t\t\tfmt.Println(\"cost:\", p.cost)\n\t\t\t\tfmt.Println()\n\t\t\t}\n\t\t\tbreak\n\t\tcase \"b\":\n\t\t\tvar toBuy string\n\t\t\tfmt.Print(\"What product would you like to buy? \")\n\t\t\tfmt.Scanln(\u0026toBuy)\n\n\t\t\tloc := -1\n\t\t\tfor i, p := range inventory {\n\t\t\t\tif p.name == toBuy {\n\t\t\t\t\tloc = i\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif loc == -1 {\n\t\t\t\tfmt.Println(\"Item isn't in stock\")\n\t\t\t} else {\n\t\t\t\tinventory = append(inventory[:loc], inventory[loc+1:]...)\n\t\t\t}\n\n\t\t\tbreak\n\t\tcase \"a\":\n\t\t\tvar newName string\n\t\t\tfmt.Print(\"Enter a product name: \")\n\t\t\tfmt.Scanln(\u0026newName)\n\n\t\t\tvar newCost int\n\t\t\tfmt.Print(\"Enter a cost: \")\n\t\t\tfmt.Scanf(\"%d\", \u0026newCost)\n\n\t\t\tinventory = append(inventory, product{name: newName, cost: newCost})\n\t\t\tbreak\n\t\t}\n\n\t\tfmt.Println(\"Welcome to the store!\")\n\t\tfmt.Println(\"Options:\")\n\t\tfmt.Println(\"(i)nventory\")\n\t\tfmt.Println(\"(b)uy item\")\n\t\tfmt.Println(\"(a)dd item\")\n\t\tfmt.Println(\"(q)uit\")\n\t\tfmt.Scanln(\u0026choice)\n\t}\n}\n```\n\nThe completed code can also be found in the `main.go` file in this repository\n\n## Further reading\n\nIf you are interested in learning more about Go, I would recommend checking out these resources:\n* [A Tour of Go](https://tour.golang.org)\n* [Go by Example](https://gobyexample.com/)\n\nAnother great resource in the official docs for when you're more familiar with the language:\n* [Effective Go](https://golang.org/doc/effective_go.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhacksu%2Fintro_to_go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhacksu%2Fintro_to_go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhacksu%2Fintro_to_go/lists"}