Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "math"
- )
- const SENIOR int32 = 65
- type Persona struct {
- name string
- lastName string
- age int32
- }
- // Defining one method for `Persona`
- func (p Persona) getFullName() string {
- return fmt.Sprintf("%s, %s", p.lastName, p.name)
- }
- func (p Persona) isSenior() bool {
- return p.age >= SENIOR
- }
- func (p *Persona) becomeSenior() {
- p.age = 65
- }
- func byval(a int, b int) {
- a = 2
- b = 3
- }
- func byref(a *int, b *int) {
- *a = 3
- *b = 4
- }
- // Interfaces let you use duck typing like you would in a purely dynamic
- // language like Python
- type ReadCloser interface {
- Read(b []byte) (n int, err int)
- Close()
- }
- // The code that calls `ReadAndClose` can pass a value of any type as long as it
- // has `Read` and `Close` methods with the right signatures
- func ReadAndClose(r ReadCloser, buf []byte) (n int, err int) {
- for len(buf) > 0 && err == 0 {
- var nr int
- nr, err = r.Read(buf)
- n += nr
- buf = buf[nr:]
- }
- r.Close()
- return
- }
- // Interfaces aren't restricted to static checking. You can check dynamically
- // whether a particular interface value has an additional method.
- type Stringer interface {
- String() string
- }
- func ToString(any interface{}) string {
- return "???"
- }
- type geometry interface {
- area() float64
- perim() float64
- }
- type rect struct {
- width, height float64
- }
- type circle struct {
- radius float64
- }
- // In order to implement an interface in Go, we just need to implement all the
- // methods in the interface
- func (r rect) area() float64 {
- return r.width * r.height
- }
- func (r rect) perim() float64 {
- return 2*r.width + 2*r.height
- }
- func (c circle) area() float64 {
- return math.Pi * c.radius * c.radius
- }
- func (c circle) perim() float64 {
- return 2 * math.Pi * c.radius
- }
- // If a variable has an interface type, then we can call methods that are in
- // the named interface
- func measure(g geometry) {
- fmt.Println(g)
- fmt.Println(g.area())
- fmt.Println(g.perim())
- }
- // Variadic functions
- func printNumbers(nums ...int) {
- fmt.Println(nums)
- }
- // Main entry point
- func main() {
- a := 1
- b := 2
- fmt.Println(a)
- fmt.Println(b)
- byref(&a, &b)
- fmt.Println(a)
- fmt.Println(b)
- // static array
- var arr [7]int
- arr[4] = 5
- fmt.Println(arr)
- // slice: A slice is a segment of an array. Like arrays slices are indexable
- // and have a length. Unlike arrays this length is allowed to change.
- // Slices can be created with the built-in make function; this is how you
- // create dynamically-sized arrays
- ss := make([]int, 10)
- ss[8] = 2
- fmt.Println(ss)
- // Using a new dynamic size array. No need to use `make`
- var tt []int
- tt = append(tt, 3)
- fmt.Println(tt)
- var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
- for i, v := range pow {
- fmt.Println(i)
- fmt.Println(v)
- }
- for _, v := range pow {
- fmt.Println(v)
- }
- // hashtable, dictionary => maps
- testMap := make(map[string]int)
- testMap["hello"] = 10
- testMap["bye"] = 20
- _, exists := testMap["hello"]
- if exists {
- fmt.Println("hello exists in hash")
- }
- for k, v := range testMap {
- fmt.Printf("%v: %v\n", k, v)
- result := fmt.Sprintf("%s => %d", k, v)
- fmt.Println(result)
- }
- // Function literals are closures
- xBig := func() bool {
- // `a` was defined before so closure can use it here
- return a > -1
- }
- fmt.Println("xBig: ", xBig())
- // Structs
- john := Persona{"John", "Smith", 27}
- fmt.Println(john)
- fmt.Println(john.getFullName())
- fmt.Println(john.isSenior())
- john.becomeSenior()
- fmt.Println(john.isSenior())
- // Interfaces
- rr := rect{width: 3, height: 2}
- cc := circle{radius: 5}
- measure(rr)
- measure(cc)
- // Calling to variadic func
- printNumbers(1, 2)
- printNumbers(1, 2, 3)
- }
Add Comment
Please, Sign In to add comment