Statically types language Variable must have a type of the value or inferred Strongly typed language Can not add string & integer Go is complied Compiler translates your code into machine code 120X Faster than python of run 1M loop Go uses UTF-8 encoding Fast compile time Build in concurrency No need to packages for parallelism Using Goroutines Simplicity Garbage collection
What is a Package? Package is a collection of Go files in a single location. E.g file_1.go, file_2.go What is a module? Collection of packages is called module. package main It is the main entry point for the package. import "fmt" You can import packages using this in a go file Go will never let us import unused packages func main() { } This function is the main entry point or where the execution starts
- You do not have to put semicolons at the end of a statement.
- Windows will generated .exe and linux/macOS will generate binary file
- Build command will just build the file & run will build and run the file
- You can not add int & float or multiply int32 with int64, use type casting
- With "Hello world" string you can not go to new line. Use
Hello worldor \n - Variable decleared without a value will default to 0, string to '' & boolean to false
- Without a value type with a variable, the type will be inferred
- Default value for error is nil
- := using for variable declearation is most populer
- All function decleared { must be next to it's name or parentheses
- Function's parameter's type can not be changed
- Use cap() to return the capacity of the slice
- %v is used to value and %T is used to print type
Installation & Create project $ go mod init name-of-module (name can be a small or location in a git repo) It will create a file name go.mod mod file will contian module name & version Other's mods & version will showup here
$ mkdir cmd/tutorial_1 $ touch main.go $ nano main.go In this file add 2 lines & a function main() package main import ("fmt", "unicode/utf8") func main(){ fmt.Println("Hello world") }
[Build the project then run] $ go build main.go or go build ./cmd/tutorial_1/main.go Both commands will build the main.go file This is generate a .exe (windows) or binary file (linux or macOS) $ ./main.exe You can run the .exe or binary file using the generated file name
[Build & run the project] $ go run main.go or go run ./cmd/tutorial_1/main.go
Tutorials 1 - What is mentioned above, how all works 2 - Conts, var & datatypes 3 - Functions and Control Structures 4 - Arrays, Slices, Maps and Loops 5 - Strings, Runes and Bytes 6 - Structs and Interfaces 7 - Pointers 8 - Goroutines 9 - Channels 10 - Generics 11 - Building an API!