Udemy: Go Bootcamp
Code: https://github.com/inancgumus/learngo
Configure VS Code
Open VS Code; from the extensions tab at the left, search for "go" and install it
Close VS Code completely and open it up again
Go to View menu; select Command Palette
- Or just press cmd+shift+p
Type: go install
- Select "Go: Install/Update Tools"
- Check all the checkboxes
After it's done, open the Command Palette again
- Type: shell
- Select: "Install 'code' command in PATH"
NOTE: You don't have to do this if you're on Windows.
7. What is GOPATH?
go env
to see all go env set
go env PATH
to see the $GOPATH
$GOPATH
in $HOME/go
10. Compile with "go build"
go build
compiles your code
./mygocode
to run after compile
go help build
to see help
12. Run with "go run"
go run
compiles and runs your code
go help run
to see help
go run -x myprog.go
will show additional info e.g. package imports
13. ★ FIRST GO PROGRAM EXERCISES ★
🚢 3d10061
Exercise: fmt.Println
diff --git a/learngo/02-write-your-first-program/exercises/01-print-names/main.go b/learngo/02-write-your-first-program/exercises/01-print-names/main.go
index 5972806..4f228f5 100644
--- a/learngo/02-write-your-first-program/exercises/01-print-names/main.go
+++ b/learngo/02-write-your-first-program/exercises/01-print-names/main.go
@@ -8,6 +8,8 @@
package main
+import "fmt"
+
// ---------------------------------------------------------
// EXERCISE: Print names
//
@@ -24,6 +26,6 @@ package main
// ---------------------------------------------------------
func main() {
- // ?
- // ?
+ fmt.Println("Arafat")
+ fmt.Println("Sally")
}
🚢 b745f3e
fmt.Prinln
and Print
can take multiple params
diff --git a/learngo/02-write-your-first-program/exercises/01-print-names/main.go b/learngo/02-write-your-first-program/exercises/01-print-names/main.go
index 4f228f5..d4c7def 100644
--- a/learngo/02-write-your-first-program/exercises/01-print-names/main.go
+++ b/learngo/02-write-your-first-program/exercises/01-print-names/main.go
@@ -26,6 +26,6 @@ import "fmt"
// ---------------------------------------------------------
func main() {
- fmt.Println("Arafat")
+ fmt.Print("My name is ", "Arafat\n")
fmt.Println("Sally")
}
🚢 ccb74ac
fmt.Println
will accept double quote and backtick, but not single quote
diff --git a/learngo/02-write-your-first-program/exercises/01-print-names/main.go b/learngo/02-write-your-first-program/exercises/01-print-names/main.go
index d4c7def..f0a7b10 100644
--- a/learngo/02-write-your-first-program/exercises/01-print-names/main.go
+++ b/learngo/02-write-your-first-program/exercises/01-print-names/main.go
@@ -26,6 +26,6 @@ import "fmt"
// ---------------------------------------------------------
func main() {
- fmt.Print("My name is ", "Arafat\n")
+ fmt.Print(`My name is Arafat\n`)
fmt.Println("Sally")
}
package
and import
declarations must come at top
14. ⭐️ Packages ⭐️
17. What is a package?
Rules
- all package files should be in the same directory
- all files in the same folder should be in the same package
- ie
package "mypkg"
- ie
package "main"
to make it executable
18. Learn the differences between Executable and Library Packages
An executable package
- should be part of
package "main"
- also include a
func main()
- cannot be imported
A Library Package
- has a
package "mypackage"
- cannot be executable directly
- can be
import "mypackage"
in other packages
19. Scopes: What is the importance of names?
There are package, file, func, and block scopes that are unique within the scope
package "main"
import "fmt" // file scoped (visible in this file)
const ok = true // package scoped: visible to all files in the package
// not available to other packages unless imported
func nope() {
const notok = false
}
func main() {
var hello = "Hello!" // block scoped declaration
fmt.Println(hello, ok) // "ok" from declaration above
fmt.Println(hello, notok) // ERR: notok declared in func nope() scope
}
21. The same names in the same package
xxx