Skip to content

MKaczkow/go_concepts

Repository files navigation

Go Concepts

Repo for basic tutorial-based Golang study

Learning Go Testing CI
Web App Bis CI
Regex Engine CI
Monkey Interpreter CI
Monkey Compiler CI Coverage


todo

  • small cleanup
  • monkey
    • compiler
    • macros
    • interpreter
  • regex engine
  • web crawler with colly
  • do gilded rose kata
  • smth with Hanoi tower
  • finish book
  • fix web_app_bis (if no JSON is provided, some routes crash)
  • init book
  • clean issues section (for now)
  • fix github actions
  • get book "Język Go. Tworzenie idiomatycznego kodu w praktyce" example link other link
  • finish web_app_bis tutorial (mostly done, need to check if everything works OK)

basic usage

  • cd app_name
  • go build -v ./
  • go test -v ./
  • go run main.go

based on

booking app tutorial (booking app + some basics)
basics tutorial (most basics)
web app tutorial (web app + Gin)
web app bis tutorial (web app + Gin)

issues

  • How should modules / packages be organized? How are they organized in real-life large projects? docs
  • How specifically, does hash maps work in Golang?
  • (DONE) func (u *UserService) CreateUser(user *models.User) What does the (u *UserService) part mean? Likely, it specifies return type, it so? (CreateUser(user *models.User) part specifies the return type)
  • (DONE) func (u *UserService) CreateUser(user *models.User) What does * mean? It looks like a pointer... (yep, it's a pointer)
  • (DONE) if err := ctx.ShouldBindJSON(&user); err != nil {...} What about ShouldBindJSON? (the syntax means, that we first create err variable, assign value to it and only then check if function returned any errors)
  • (DONE) query := bson.D{bson.E{Key: "name", Value: name}} What is bson.D or bson.E? (these are two out of four basic types of BSON documents, which are the basic unit of data in MongoDB, ref: https://www.mongodb.com/docs/drivers/go/current/fundamentals/bson/)
  • update := bson.D{primitive.E{Key: "$set", Value: bson.D{
      	primitive.E{Key: "name", Value: user.Name}, 
      	primitive.E{Key: "age", Value: user.Age}, 
      	primitive.E{Key: "address", Value: user.Address},
      }}}
    
    Why changging Bson.E -> primitive.E fixed erorrs ?? (from https://pkg.go.dev/go.mongodb.org/mongo-driver/bson:
    M - unordered representation of a BSON document
    D - ordered representation of a BSON document
    E - a BSON element for a D
    )
  • (DONE) difference between = and := in Golang? (:= means 'declare and assign', while = means 'assign')
  • (DONE) can you return empty values in Golang? (yes, you can return empty values in Golang, in fact it's quite often a prefered way of doing things - so return nil or just return and don't bother)

notes

  • golang function syntax:
func functionName(parameter1 type1, parameter2 type2, parameterN typeN) returnType {
	   //function body
}
  • golang comparision of objects also takes into account the actual data location in memory, so two objects with the same values, but different memory locations are not equal (example from Writing an Interpreter in Go):
name1 := &object.String{Value: "name"}
monkey := &object.String{Value: "Monkey"}
pairs := map[object.Object]object.Object{}

pairs[name1] = monkey
fmt.Printf("pairs[name1]=%+v\n", pairs[name1])
// => pairs[name1]=&{Value:Monkey}

name2 := &object.String{Value: "name"}
fmt.Printf("pairs[name2]=%+v\n", pairs[name2])
// => pairs[name2]=<nil>
fmt.Printf("(name1 == name2)=%t\n", name1 == name2)
// => (name1 == name2)=false

cool resources