Projects & Modules

TaskCode ExampleDescription
Creating a new go project
mkdir myproject
cd myproject
go mod init myproject

link

https://go.dev/ref/mod#go-mod-init

Download modulesgo mod vendorhttps://go.dev/ref/mod#go-mod-vendor

Cleanup modules

go mod tidy

Ensures that the go.mod file matches the source code in the module.

https://go.dev/ref/mod#go-mod-tidy


Building

TaskCode ExampleDescription
Build

go build  -o mysvc

Build project


Types/Objects

TaskCode ExampleReference
Creating an instance of a struct
type Student struct {
	Name string
	Age  int
}


b := Student{ Name: "Bob", }
p := &Student{ Name: "Bob", } //pointer to Student

link
Creating an instance of a struct
var pa *Student   // pa == nil
pa = new(Student) // pa == &Student{"", 0}
pa.Name = "Alice" // pa == &Student{"Alice", 0}

Implementing an Interface
package main
import "fmt"

type I interface {
	M()
}

type T struct {
	S string
}

// This method means type T implements the interface I,
// but we don't need to explicitly declare that it does so.
func (t T) M() {
	fmt.Println(t.S)
}

func main() {
	var i I = T{"hello"}
	i.M()
}


link


References

ReferenceURL

Go Home

https://go.dev

Go Documentation

https://go.dev/doc/

Tutorials

https://go.dev/doc/tutorial/

Go by Example

https://gobyexample.com
  • No labels