- Code runs faster
- Garbage collections
- Simpler objects
- Concurrency is inbuilt which improves efficiency using **goroutines
- Channels help to solve this problem by providing a pattern that makes data safe from concurrent modification. Channels help to enforce the pattern that only one goroutine should modify the data at any time.
- The first goroutine passes a data value through the channel to a second goroutine that’s already waiting. The exchange of the data between both goroutines is synchronized, and once the hand-off occurs, both goroutines know the exchange took place.
- Go uses composition (inheritance vs composition)
- Rather than building a long inheritance structure—Client extends User extends Entity—Go developers build small types—Customer and Admin and embed them into larger ones.
- Go has a modern garbage collector that does the hard work for you.
- C++ & C, you need to allocate a piece of memory before you can use it, and then free it when you’re done. If you fail to do either of these correctly, you’ll have program crashes or memory leaks.
- Machine language: Cpu instructions represented in binary
- Assembly language: CPU instructions with Mnemonics - easy to read
- High level Language: Commonly used language (C, C++, Java, Python, Go etc) - easy to use.
- Basics of GO Lang has been given each with an example
- Variable, Strings, Conditions
- Go-lang downloads: https://golang.org/
- Creating workspaces ,writing code and testing: https://golang.org/doc/code#Workspaces
- Various docs on go-lang: https://blog.golang.org/godoc
- GO-by-Examples
-
GOPATH
- Path of the package modules which are download or go compiler to search for package location or environment variable lists places to look for Go code -
GOROOT
- Go binary distributions assume they will be installed in /usr/local/go, but it is possible to install the Go tools to a different location. In this case you must set the GOROOT environment variable to point to the directory in which it was installed. -
HOW TO EXECUTE
go run filename.go
-
go get "https://github.com/aws/aws-sdk-go/tree/main/service/s3"
this will download the required go packages to the current location. -
go get -u https://github.com/aws/aws-sdk-go/...
All the AWS related GO modules will be downloaded. -
godoc fmt
to get more about fmt pkg -
go get
can be used to get the pkg if not present in local GOPATH location. -
BLANK IDENTIFIER The _
-
init functions that are discovered by the compiler are scheduled to be executed prior to the main function -
go clean hello.go
After calling clean, the executable program is gone
-
1st type = var a int \ a= 55
-
2nd type = var b int = 66
-
3rd type = c := 77
-
map
is an inbuilt data type in go lang. structure of mapmake(map (key-type]value-type)
Make to initialize for the first time. -
Pointers * for pointers, & reference
x :=5
y := &x
fmt.println(x,y)
x :=5
y := &x ---- pointer of x
*y = 6 ----- y pointer with 6 pointer (memory location) x's memory location changed to y
fmt.println(x,*y)
Output = 6, 6
- imports - imports code and give access to identifiers such as functions, constants and interfaces.
- log and OS are inbuilt function.
- A Unit compiled code is called package.
_ "github.com/goinaction/code/chapter2/sample/matchers
- Technique to initialization from package even if you are not directly using, it will not import if not used. But init function calls if different code file within that package has it. - ONE WORD, imports if required only.- INIT code would be called if defined before the main function. Even main is starting of the program but INIT will take the preceding.
- For Variables which can directly used will get error. So we should use map and assign it to your variable
- All variables are initialized to their Zero Value.
make(map[string]Matcher)
- A map is a reference type that you’re required to make in Go- Numeric type value = 0
- Strings type value = empty string
- Booleans type value = false
- Pointers type value = nil
- reference type values = nil Zero - Nil
package main
import (
"log"
"os"
_ "github.com/goinaction/code/chapter2/sample/matchers"
"github.com/goinaction/code/chapter2/sample/search"
)
// init is called prior to main.
func init() {
// Change the device for logging to stdout.
log.SetOutput(os.Stdout)
}
// main is the entry point for the program.
func main() {
// Perform the search for the specified term.
search.Run("president")
}
-
":=" This operator is used to both declare and initialize variables at the same time.
-
Use the keyword go to launch and schedule goroutines to run concurrently. Anonymous function as a goroutine, function that’s declared without a name is call Anonymous. In below code each feed be processed independently in a concurrent fashion.
- Functions accept a type Matcher and the address of a value type Feed. feed is a pointer variable. Pointer variables are grate to share variables between functions. They also allows function to access and change the state of the variable that we declared with in the scope of different functions and possible different goroutine.
go func(matcher Matcher, feed *Feed) {
Match(matcher, feed, searchTerm, results)
waitGroup.Done()
}(matcher, feed)