https://godoc.org/runtime#Caller
Useful for error logging.
https://godoc.org/runtime#Caller
Useful for error logging.
Reference:
https://golang.org/src/database/sql/sql.go?s=4943:5036#L178
type NullString struct {
String string
Valid bool // Valid is true if String is not NULL
}
if !reflect.ValueOf(strct).Field(i).CanInterface() {
continue
}
type Point struct {
_ [0] func()
X, Y int
}
package main
import "fmt"
func main() {
for i := range (*[10]int)(nil) { // int can be replaced with any type
fmt.Println(i)
}
for i := range [10][0]int{} { // int can be replaced with any type
fmt.Println(i)
}
for i := range [10]struct{}{} {
fmt.Println(i)
}
}
package main
import "time"
func main() {
go func() {
for {
time.Sleep(time.Second)
}
}()
select{} // block here for ever
}
const MaxUint = ^uint(0) const MaxInt = int(^uint(0) >> 1) const Is64bitOS = ^uint(0) >> 63 // 1 or 0 const WordBits = 32 << (^uint(0) >> 63) // 64 or 32 import "unsafe" const Is64bitOS = unsafe.Sizeof(1) / 8 const WordBits = unsafe.Sizeof(1) * 8
type I interface {
f()
}
func _() {
var _ I = T{} // assert T implements I
var _ I = (*T)(nil) // assert *T implements I
}
type T struct{}
func (t T) f() {}
const M = 8
const N = 8
func _() {
// methods to assert N >= M
var _ [N-M]int
type _ [N-M]int
const _ uint = N-M
// methods to assert M == N
var _ [M-N]int; var _ [N-M]int
type _ [M-N]int; type _ [N-M]int
const _, _ uint = M-N, N-M
}
package main
import (
"time"
"fmt"
)
func neverExist() {
time.Sleep(time.Second)
panic("crashed.")
}
func autoRestart(f func()) {
defer func() {
recover()
fmt.Println("restart")
go autoRestart(f)
}()
f()
}
func main() {
go autoRestart(neverExist)
select{}
}
import "database/sql"
func Exec(db *sql.DB, sqlstr string) error {
// using db ...
// ...
// do transaction
tx, err := db.Begin()
if err != nil {
return err
}
// sql.DB and sql.Tx have many same methods,
// to avoid misusing the db variable below,
// we can shaddow it.
{
type db complex64 // shaddow db
// _, err := db.Exec(sqlstr)
// if we call the above line, compiler will report error.
_, err := tx.Exec(sqlstr)
if err != nil {
tx.Rollback()
return err
}
return tx.Commit()
}
}