Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
Code Block
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