Skip to content

Instantly share code, notes, and snippets.

View iporsut's full-sized avatar
🏠
Working from home

Weerasak Chongnguluam iporsut

🏠
Working from home
View GitHub Profile
@iporsut
iporsut / gist:57894820d97c4cd0c0c4
Created March 2, 2015 14:44
Goroutine SecondCounter
package main
import (
"fmt"
"time"
)
func SecondCounter() {
for i := 1; i <= 20; i++ {
time.Sleep(1 * time.Second)
@iporsut
iporsut / gist:c161e3d75b60c05af927
Created March 2, 2015 15:01
Goroutine SecondCounter Channel Sync
package main
import (
"fmt"
"time"
)
var done chan bool = make(chan bool)
func SecondCounter() {
@iporsut
iporsut / gist:c1e5e62ad1d5411b7bf6
Created March 2, 2015 15:07
Goroutine SecondCounter and FiveSecondCounter Channel Sync
package main
import (
"fmt"
"time"
)
var oneDone chan bool = make(chan bool)
var fiveDone chan bool = make(chan bool)
package main
import (
"errors"
"fmt"
)
func Div(a, b float32) (result float32, err error) {
if b == 0 {
err = errors.New("Cannot divide by zero.")
@iporsut
iporsut / gist:a727a0f7cfb8fbf751e8
Created March 7, 2015 13:39
Insert to MongoDB
package main
import (
"log"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func main() {
@iporsut
iporsut / gist:2f03a716833ea7bfa044
Created March 8, 2015 08:15
go test Example from json format
func ExampleJsonFormat() {
r := bytes.NewReader([]byte(`{"name":"Weerasak"}`))
formated, _ := From(r)
fmt.Println(formated)
// Output:
// {
// "name": "Weerasak"
// }
}
package main
import "fmt"
func space(ch rune, end rune) (side int, in int) {
if ch == 'A' {
in = 0
} else {
in = int((ch-'A')*2 - 1)
}
type Account struct {
Name string
Balance float64
}
@iporsut
iporsut / gist:774c85c3858a0c8403bd
Created May 4, 2015 09:49
Account String Method
func (acc Account) String() string {
return fmt.Sprintf("Account %s have balance %0.2f", acc.Name, acc.Balance)
}
@iporsut
iporsut / gist:c572df037a573adf6700
Created May 4, 2015 09:51
Account use String Method
func main() {
acc := Account{Name: "Weerasak", Balance : 1000.00 }
fmt.Println(acc.String())
pacc := &acc
fmt.Println(pacc.String())
}