Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Last active March 18, 2020 23:20
Show Gist options
  • Select an option

  • Save danidiaz/43b6a8648f528040a61442813abe48de to your computer and use it in GitHub Desktop.

Select an option

Save danidiaz/43b6a8648f528040a61442813abe48de to your computer and use it in GitHub Desktop.
// https://www.golang-book.com/books/intro
package main
import ("fmt"
"reflect")
func main() {
fmt.Printf("hello, world\n")
//var foo string = "foooo"
var x [5]int
x[3] = 4
for i := 0; i < len(x); i++ {
fmt.Println(x[i])
}
for _, v := range x {
fmt.Println(v)
}
z := 3
if z == 3 {
fmt.Println("is 0")
} else if z == 1 {
fmt.Println("is ", z)
}
switch z {
case 0:
fmt.Println("0")
case 2:
fmt.Println("1")
default:
fmt.Println("x")
}
fmt.Println("---")
somefunc()
fmt.Println("---")
somefunc2()
fmt.Println("---")
somefunc3()
a, b := somefunc4(3)
fmt.Println(a,b)
fmt.Println("---")
fuffy := makeEventGenerator()
fmt.Println(fuffy())
fmt.Println(fuffy())
fmt.Println(fuffy())
paniquea()
fmt.Println("-------checking interfaces")
checkInterfaceAssign1()
checkInterfaceAssign2()
checkInterfaceAssign3()
checkInterfaceAssign4()
}
func somefunc() {
arr := [5]int{1, 2, 3, 4, 5}
for _, x := range arr {
fmt.Println(x)
}
}
func somefunc2() {
arr := [5]int{1,
2,
3,
4,
5,
}
for _, x := range arr {
fmt.Println(x)
}
}
func somefunc3() {
var arr map[int]int
arr = make(map[int]int)
arr[1] = 3
fmt.Println(arr[1])
if name, ok := arr[1]; ok {
fmt.Println("Found in map!")
fmt.Println(name,ok)
}
}
func somefunc4(cosa int) (int,int) {
return cosa + 5, cosa + 2
}
func makeEventGenerator() func() uint {
var i uint
i = 0
return func() uint {
var retty uint
retty = i
i+= 2
return retty
}
}
func paniquea() {
defer func() {
str := recover()
fmt.Println("recovering from a panic",str)
}()
panic("this is a panic")
}
// https://www.golang-book.com/books/intro/9
// https://golangbot.com/structs-instead-of-classes/
// https://stackoverflow.com/questions/18125625/constructors-in-go/18125763
type Square struct {
x int
y int
}
func (sq *Square) area() int {
return sq.x * sq.y
}
// https://dev.to/chen/gos-method-receiver-pointer-vs-value-1kl8
// https://stackoverflow.com/questions/40823315/x-does-not-implement-y-method-has-a-pointer-receiver
// https://golang.org/ref/spec#Method_sets
// https://stackoverflow.com/questions/13511203/why-cant-i-assign-a-struct-to-an-interface
func (sq Square) area2() int { // whaaaat
return sq.x * sq.y
}
func doSquareStuff() {
var sq Square
//sq = new(Square)
sq = Square{x:0,y:0}
fmt.Println("the square v1",sq,sq.area())
fmt.Println("the square v2",sq,sq.area2())
}
type Shape interface {
area() int
}
type Shape2 interface {
area2() int
}
type Bigger struct {
shape Shape
foo int
}
// https://stackoverflow.com/questions/14663774/why-assign-a-reference-to-a-struct-in-go
func doBiggerStuff() {
// sq := Square{0,1}
var sh Shape
sh = &Square{0,1}
x := Bigger{shape:sh,foo:20}
y := Bigger{shape:new(Square),foo:20}
fmt.Println("bigger",x,y)
var sh2 Shape2
sh2 = Square{0,1} // whaaat interfaces are not like pointers? https://stackoverflow.com/a/13511853
fmt.Println(sh2)
}
// https://medium.com/golangspec/interfaces-in-go-part-i-4ae53a97479c
// https://npf.io/2014/05/intro-to-go-interfaces/ “two pointers”
// https://blog.friendsofgo.tech/posts/como-usar-reflection-en-golang/
// https://medium.com/@saiyerram/go-interfaces-pointers-4d1d98d5c9c6
func checkInterfaceAssign1() {
var sq Square
sq = Square{1,1}
var sh Shape2
sh = sq
sq.x = 22
fmt.Println("* ",sh)
fmt.Println(1,reflect.TypeOf(sh))
}
func checkInterfaceAssign2() {
var sq Square
sq = Square{1,1}
var sh Shape2
sh = &sq
sq.x = 22
fmt.Println("* ",sh)
fmt.Println(2,reflect.TypeOf(sh))
}
// “The interaction between nil interfaces and nil pointers is where nearly everyone gets tripped up when they first start with Go.”
func checkInterfaceAssign3() {
var sh Shape2
sh = nil
fmt.Println(3,reflect.TypeOf(sh))
}
func checkInterfaceAssign4() {
var sh Shape2
var sq *Square = nil
sh = sq
fmt.Println(4,reflect.TypeOf(sh))
// panic: value method main.Square.area2 called using nil *Square pointer
// fmt.Println("area ",sh.area2())
}
package main
import ("fmt"
// "reflect"
"time"
)
func main() {
c := make(chan int)
finish := make(chan string)
go printGos(c,finish)
for i := 0; i < 10 ; i++ {
c <- i
time.Sleep(1 * time.Second)
}
finish <- "The end!"
<-finish
fmt.Println("Main thread!")
}
func printGos(c <-chan int, finish chan string) {
for {
select {
case msgi := <-c:
fmt.Println(msgi)
case <- finish: {
finish <- "Acknlowledged!"
break;
}
}
}
}
// Hugely inefficient, I know.
// Only a silly example to learn the "sort" package.
// https://www.golang-book.com/books/intro
package main
import ("fmt"
"sort")
type Listy struct {
Val int
Next *Listy
}
func Empty() *Listy {
return nil
}
// https://yourbasic.org/golang/structs-explained/
func Cons(x int,xs *Listy) *Listy{
r := &Listy{x,xs}
return r
}
func PrintListy(xs *Listy) {
zs := xs
for zs != nil {
fmt.Println(zs.Val)
zs = zs.Next
}
}
func (xs *Listy) Len() int {
zs := xs
i := 0
for ; zs != nil; i++ {
zs = zs.Next
}
return i
}
func (xs *Listy) Less(i,j int) bool {
xi := GetAt(i,xs)
xj := GetAt(j,xs)
return xi < xj
}
func (xs *Listy) Swap(i,j int) {
xi := GetAt(i,xs)
xj := GetAt(j,xs)
SetAt(i,xj,xs)
SetAt(j,xi,xs)
}
func SetAt(i int, v int,xs *Listy) {
zs := xs
for index := 0; index < i; index++ {
zs = zs.Next
}
zs.Val = v
}
func GetAt(i int, xs *Listy) int {
zs := xs
for index := 0; index < i; index++ {
zs = zs.Next
}
return zs.Val
}
func ExampleListy() *Listy {
return Cons(7,Cons(1,Cons(3,Cons(10,Cons(2,Empty())))))
}
func main() {
fmt.Println("foo")
xs := ExampleListy()
PrintListy(xs)
fmt.Println("--")
fmt.Println(GetAt(2,xs))
SetAt(2,4,xs)
fmt.Println(GetAt(2,xs))
fmt.Println("--")
fmt.Println(xs.Len())
fmt.Println("--")
sort.Sort(xs)
PrintListy(xs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment