mas ejemplos
This commit is contained in:
		
							
								
								
									
										53
									
								
								gobyexample/errors.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								gobyexample/errors.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,53 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func f1(arg int) (int, error) {
 | 
			
		||||
	if arg == 42 {
 | 
			
		||||
		return -1, errors.New("can't work with 42")
 | 
			
		||||
	}
 | 
			
		||||
	return arg + 3, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type argError struct {
 | 
			
		||||
	arg int
 | 
			
		||||
	prob string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (e *argError) Error() string {
 | 
			
		||||
	return fmt.Sprintf("%d - %s", e.arg, e.prob)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func f2(arg int) (int, error) {
 | 
			
		||||
	if arg == 42 {
 | 
			
		||||
		return -1, &argError{arg,  "can't work with it"}
 | 
			
		||||
	}
 | 
			
		||||
	return arg + 3, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	for _, i := range []int{7 ,42} {
 | 
			
		||||
		if r, e := f1(i); e != nil {
 | 
			
		||||
			fmt.Println("f1 failed:", e)
 | 
			
		||||
		} else {
 | 
			
		||||
			fmt.Println("f1 worked:", r)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, i := range []int{7, 42} {
 | 
			
		||||
		if r,e := f2(i); e != nil {
 | 
			
		||||
			fmt.Println("f2 failed:", e)
 | 
			
		||||
		} else {
 | 
			
		||||
			fmt.Println("f2 worked:", r)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, e := f2(42)
 | 
			
		||||
	if ae, ok := e.(*argError); ok {
 | 
			
		||||
		fmt.Println(ae.arg)
 | 
			
		||||
		fmt.Println(ae.prob)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										46
									
								
								gobyexample/interfaces.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								gobyexample/interfaces.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,46 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type geometry interface {
 | 
			
		||||
	area() float64
 | 
			
		||||
	perim() float64
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type rect struct {
 | 
			
		||||
	width, height float64
 | 
			
		||||
}
 | 
			
		||||
type circle struct {
 | 
			
		||||
	radius float64
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func measure(g geometry) {
 | 
			
		||||
	fmt.Println(g)
 | 
			
		||||
	fmt.Println(g.area())
 | 
			
		||||
	fmt.Println(g.perim())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	r := &rect{width:3, height:4}
 | 
			
		||||
	c := &circle{radius: 5}
 | 
			
		||||
 | 
			
		||||
	measure(r)
 | 
			
		||||
	measure(c)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								gobyexample/methods.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								gobyexample/methods.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,26 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
type rect struct {
 | 
			
		||||
	width, height int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *rect) area() int {
 | 
			
		||||
	return r.width * r.height
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r rect) perim() int {
 | 
			
		||||
	return 2*r.width + 2*r.height
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	r := rect{width: 10, height: 5}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("area:", r.area())
 | 
			
		||||
	fmt.Println("perim:", r.perim())
 | 
			
		||||
 | 
			
		||||
	rp := &r
 | 
			
		||||
	fmt.Println("area:", rp.area())
 | 
			
		||||
	fmt.Println("perim:", rp.perim())
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								gobyexample/pointers.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								gobyexample/pointers.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func zeroval(ival int) {
 | 
			
		||||
	ival = 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func zeroptr(iptr *int) {
 | 
			
		||||
	*iptr = 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	i := 1
 | 
			
		||||
	fmt.Println("initial:", i)
 | 
			
		||||
 | 
			
		||||
	zeroval(i)
 | 
			
		||||
	fmt.Println("zeroval:", i)
 | 
			
		||||
 | 
			
		||||
	zeroptr(&i)
 | 
			
		||||
	fmt.Println("zeroptr:", i)
 | 
			
		||||
 | 
			
		||||
	fmt.Println("pointer:", &i)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								gobyexample/structs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								gobyexample/structs.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
type person struct {
 | 
			
		||||
	name string
 | 
			
		||||
	age int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func newPerson(name string) *person {
 | 
			
		||||
	p := person{name:name}
 | 
			
		||||
	p.age = 42
 | 
			
		||||
	return &p
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
 | 
			
		||||
	fmt.Println(person{"Bob", 20})
 | 
			
		||||
 | 
			
		||||
	fmt.Println(person{name:"Alice", age:30})
 | 
			
		||||
 | 
			
		||||
	fmt.Println(person{name:"Fred"})
 | 
			
		||||
 | 
			
		||||
	fmt.Println(&person{name: "Ann", age: 40})
 | 
			
		||||
 | 
			
		||||
	fmt.Println(newPerson("Jon"))
 | 
			
		||||
 | 
			
		||||
	s := person{name:"Sean", age: 50}
 | 
			
		||||
	fmt.Println(s.name)
 | 
			
		||||
 | 
			
		||||
	sp := &s
 | 
			
		||||
	fmt.Println(sp.age)
 | 
			
		||||
 | 
			
		||||
	sp.age = 51
 | 
			
		||||
	fmt.Println(sp.age)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user