Skip to content

Instantly share code, notes, and snippets.

View olehcambel's full-sized avatar
🐍

Олег Моисеенко olehcambel

🐍
  • @[object Object]
  • Kyiv
View GitHub Profile
@olehcambel
olehcambel / pointer.go
Last active April 18, 2020 08:00
example of pointer's usage. In general, all methods on a given type should have either value or pointer receivers, but not a mixture of both.
package main
import (
"fmt"
)
type counter struct {
I int
}
package main
import (
"log"
"time"
)
func main() {
c1 := make(chan string)
c2 := make(chan string)
@olehcambel
olehcambel / interface-union.go
Created April 22, 2020 18:48
using union to merge 2 interfaces
type Reader interface {
Read(p []byte) (n int, err error)
Close() error
}
type Writer interface {
Write(p []byte) (n int, err error)
Close() error
}
package main
import (
"fmt"
"math/rand"
"time"
)
// Query a replicated database
// Teardown of late finishers is left as an exercise.
@olehcambel
olehcambel / implements.go
Last active May 5, 2020 10:24
A way to check if struct implements an interface
package main
import (
"sort"
)
type RawMessage []int
func (mes RawMessage) Len() int {
return len(mes)
@olehcambel
olehcambel / channel.go
Last active May 5, 2020 16:53
buffered/unbuffered channel impl.
package main
import (
"fmt"
"time"
)
func main() {
c := make(chan int) // Allocate a channel.
// uncomment this line
package main
import (
"fmt"
"net/http"
"time"
)
const maxOutstanding = 2
package main
import (
"fmt"
"runtime"
"time"
)
var numCPU = runtime.NumCPU()
@olehcambel
olehcambel / recover-panic.go
Created May 8, 2020 09:01
A way to recover from panic (regexp) and return error.
// Error is the type of a parse error; it satisfies the error interface.
type Error string
func (e Error) Error() string {
return string(e)
}
// error is a method of *Regexp that reports parsing errors by
// panicking with an Error.
func (regexp *Regexp) error(err string) {
panic(Error(err))
@olehcambel
olehcambel / status-checker.go
Last active May 16, 2020 14:59
check status code of websites
package main
import (
"fmt"
"net/http"
"runtime"
)
func printDomain(d string) {
fmt.Println(d)