Repository for learning GO lang

LearnGOLang

Repository for learning GO lang.

What is Go?

  • Go is a fast, statically typed compiled language
  • Compiled to machine level code, does not need to be interpretted one line at a time
  • Static typed - picks up variable types
  • General purpose language
  • Built-in testing support
  • Object-oriented langauge in it's own way different from other languages

First Go file

  • One main() function allowed in the application
  • Methods on imported packages all start with a capital letter ie) Println
  • Running the program:

go run filename.go

Variables, Strings, Numbers

  • Strings in GO must use double quotes: ""
  • Variables can be typed or automatically infer it:

var nameOne string = "mario"
var nameTwo = "luigi"
var nameThree string // Variable is initialized. If no value is given, the string is empty
nameFour := "yoshi" // Shorthand that is only used to initialize the variable the first time

  • ":=" Can't be used outside of a function

  • Ints can be typed to use bits. Prevents certain numbers to be used within the scope.

var ageOne int = 20
var ageTwo = 30
ageThree := 40
var numOne int8 = 25 // 8 bits
var numThree uint = 25 // Prevents the use of negative ints (0 - 255)

  • Floats must specify the bit-size. Bit-size dictate the range of numbers we can use

var scoreOne float32 = 25.98
var scoreTwo float64 = 3845934578734859.234234234
scoreThree := 1.5 // Shorthand inferred as float64

Printing & Formatting Strings

  • Printing variables and strings

fmt.Println("my age is", ageOne, "and the score is", scoreOne)

  • Printing formatted strings using a format specifier "%v". The order of variables matters.

fmt.Printf("my age is %v and the score is %v", ageOne, scoreOne)

  • Specifier "%q" adds quotes around the string variables
  • Specifier "%T" outputs the type of the variable
  • Specifier "%f" outputs floats. This can be rounded to the nearest decimal point

fmt.Printf("you scored %0.1f", 22.55) = "you scored 22.6"

  • Sprintf = Save printf allows ability to save the string into a variable

var str = fmt.Sprintf("my age is %v and the score is %v \n", ageOne, scoreOne)
fmt.Println("The save string is: ", str)
https://pkg.go.dev/[email protected]

Arrays and Slices

  • If the length of an array is declared, the array is a fixed and can't be changed
  • Arrays are created using curley braces instead of square brackets

var ages [3]int = [3]int{20, 25, 30} // ages will be an array of length 3 of all ints
var ages = [3]int{20, 25, 30} // Same as above
names := [4]string{"yoshi", "bowser", "mario", "wario"}
fmt.Println(ages, len(ages)) // len() grabs the length of the array

  • Slices use arrays under the hood but can manipulate arrays more
  • When no number is placed inside the square brackets, it specifies to use slices instead of arrays. They have no fixed length
  • Can append items to a slice

scores := []int{100, 50, 60}
scores[2] = 25 // Set index 2 to be value 25
scores = append(scores, 85) // append returns a new slice
fmt.Println(scores, len(scores))

  • Slice ranges:

rangeOne := names[1:3] // Includes from 1 to but not including 3
rangeTwo := names[2:] // From index 2 to the end, including end
rangeThree := names[:3] // From start to index 3 but not including 3

The Standard Library

  • Strings package

greeting := "hello there world"
fmt.Println(strings.Contains(greeting, "hello")) // returns true or false if > strings contain value
fmt.Println(strings.ReplaceAll(greeting, "hello", "Hi")) // Does not mutate original > variable
fmt.Println(strings.ToUpper(greeting))
fmt.Println(strings.Index(greeting, "ll")) // Get index of where the value starts
fmt.Println(strings.Split(greeting, " ")) // Split string into a splice by delimitter

  • Sort package

newAges := []int{45, 20, 35, 30, 75, 60, 50, 25}
sort.Ints(newAges) // Alters the original variable
fmt.Println(newAges)
fmt.Println(sort.SearchInts(newAges, 30)) // Searches slice and returns index

newStrings := []string{"one", "two", "five", "three"}
sort.Strings(newStrings)
fmt.Println(newStrings)
fmt.Println(sort.SearchStrings(newStrings, "five"))
https://pkg.go.dev/std

Owner
Allan
Software developer at Reliable Controls. I enjoy creating data visualizations and exploring new technologies.
Allan
Similar Resources

CI/CD Learning

cicd-learning CI/CD Learning /gowebapp Building Here we will cover building the application and docker image Go To build the web app, just run, from t

Nov 25, 2021

Collecting and learning common algorithms

中文 Alogrithms Learning alogrithms. Dynamic Programming Fibonacci Grid Traveler Can Sum How Sum TODO Sorting Selection Sort Bubble Sort Insertion Sort

Jan 10, 2022

Goro: A High-level Machine Learning Library for Go

Goro: A High-level Machine Learning Library for Go

Overview Goro is a high-level machine learning library for Go built on Gorgonia. It aims to have the same feel as Keras. Usage import ( . "github.

Nov 20, 2022

Golang - A collection of small Go programs used as learning exercises

This repo is a collection of small Go programs used as learning exercises Some o

Dec 31, 2021

This is a learning poem for golang~

This is a learning poem for golang~

golang-learning This will be a learning Bible ( in developing ... ) for golang~ PS : Currently, all learning content is based on Golang 1.17.5. 主要包含以下

Jan 12, 2022

This is a learning poem for golang~

This is a learning poem for golang~

golang-learning-bible This will be a learning Bible ( in developing ... ) for golang~ 中文 | English PS : Currently, all learning content is based on Go

Jan 12, 2022

Turtorial - A Hard Fork Of Icexin's Great Gocraft Project Aimed At Learning Basic Programming Tasks Through Turtles

Turtorial - A Hard Fork Of Icexin's Great Gocraft Project Aimed At Learning Basic Programming Tasks Through Turtles

TURTORIAL A voxel sandbox-survival game aimed at teaching the basics of programm

Jan 25, 2022

Golang - Learning go aka Flow with the go

golang learning go aka Flow with the go! helpful links tour of go: https://go.de

Feb 3, 2022

Learning Hexagonal Pattern with Golang

Marketplace Utility Software yang harus terinstal di komputer Software Versi Golang 1.17+ MariaDB 10.3+ Cara Menjalakan Clone repo ini Buat database d

Feb 10, 2022
Learning and Practice - Go Lang

Go Lang - Learning and Practice All the code stored here is provided by Tour of Go Basics Packages, variables, and functions Flow control statements:

Jan 14, 2022
🐨 Go lang Study Repository.

go ?? Go lang Study Repository. 01 - Hello World Hello world in GO! What have learned? Create a module using the go init command. Declare a package. I

Feb 15, 2022
The repository intent is store some Go Lang codes.

⚠️ Go programming course ⚠️ This repository was created to share the Learn How To Code: Google's Go (golang) Programming Language code created by Todd

Jan 16, 2022
This project is a collection of many of the basic tools used on Unix-like operating systems implemented in Go as a learning exercize.

GoUnix This project is a collection of many of the basic tools used on Unix-like operating systems implemented in Go as a learning exercize. The idea

Jul 21, 2022
Examples of Golang compared to Node.js for learning
Examples of Golang compared to Node.js for learning

This guide full of examples is intended for people learning Go that are coming from Node.js, although the vice versa can work too. This is not meant to be a complete guide and it is assumed that you've gone through the Tour of Go tutorial. This guide is meant to be barely good enough to help you at a high level understand how to do X in Y and doing further learning on your own is of course required.

Jan 9, 2023
100 days of Go learning
100 days of Go learning

This repository is a journal of my path to learning GO. By the end of the 100 days, you should be able to follow along by day and learn Go as well.

Oct 27, 2022
Golang Learning resources

golang Golang Learning resources Data type detection dataFields interface{} dtype := reflect.TypeOf(dataFields).Kind().String() var c Company fmt.Prin

Dec 14, 2022
Contains hundreds of samples for learning Go.
Contains hundreds of samples for learning Go.

Get Started introduction installation Basics hello scopes imports simple multiple alias lifecycle comments simple multiline documentation semicolons v

Dec 15, 2022
Learning GO language by building projects
Learning GO language by building projects

GoLangProjects Projects list Helloworld Variables Userinput Conversion Math, crypto & random Time & Date Pointers Arrays Slices Maps Structures If-Els

Dec 5, 2022
Skill builders for learning conversational Golang.

goPracticum Skill builders for learning conversational Golang. The Theory I'm stealing...err...borrowing profusely from human language learning and cu

Dec 9, 2021