Skip to content

Instantly share code, notes, and snippets.

@mtilson
mtilson / main.go
Created January 14, 2020 13:54
how to create pure++ http server (with own router and logging) - in less than 20 code lines [golang] [20lines]
package main
import (
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}
@mtilson
mtilson / main.go
Last active January 14, 2020 14:37
how to create pure echo server - in about 20+ code lines [golang] [20lines]
package main
import (
"fmt"
"net"
)
func handle(conn net.Conn) {
defer conn.Close()
var readBuf = make([]byte, 512)
@mtilson
mtilson / main.go
Last active January 14, 2020 22:58
how to count http request with goroutine and channel - in 20 code lines [golang] [20lines]
package main
import "fmt"
import "net/http"
var counterChan chan int
func counter() {
for i := 0; ; i++ { counterChan <- i }
}
@mtilson
mtilson / main.go
Last active March 10, 2020 13:50
why you have to avoid the assignment of the dysfunctional value to the interface [golang]
// https://play.golang.org/p/1hGHFwjlwud
package main
import (
"bytes"
"fmt"
"io"
)
func main() {
@mtilson
mtilson / RESULT.md
Last active March 18, 2020 19:51
how type assertions rules are applied [golang]
options: type assertions to interface value method receiver interface value type result for interface type result for interface type
case x.(T) type / pointer value / pointer var x interface{} var x InterfaceType
A concrete type type value true true
B pointer to concrete type type value false false
C interface type type value true true
D concrete type type pointer false false
E pointer to concrete type type pointer true true
F interface type type pointer true true
G concrete type pointer value true static check failure
@mtilson
mtilson / main.go
Last active March 18, 2020 20:22
confusion related to interface satisfaction of types for using 'values' VS 'pointers to value' as type method receivers [golang]
package main
import (
"fmt"
)
type InterfaceType interface {
MethodReceiverIsPtr()
MethodReceiverIsValue()
}
@mtilson
mtilson / main.go
Last active March 11, 2020 23:27
type assertion
package main
import "fmt"
type ConcreteTypeTypeReceiver struct {n int}
func (c ConcreteTypeTypeReceiver) someMethod() int {return c.n}
// declaration above SUPPORTs the following types
// 1. `ConcreteTypeTypeReceiver` - with methods (according to case C2)
// 1. `someMethod()`
@mtilson
mtilson / main.go
Last active March 18, 2020 19:52
how compiler implicitly makes reference and dereferences receiver argument for method call expression and how this influences interface satisfaction [golang]
// https://play.golang.org/p/Nolp4FTyp9I
package main
import "fmt"
type T struct {}
func (*T) String() string { // receiver parameter has type *T
return ""
}
@mtilson
mtilson / script.sh
Last active July 31, 2024 16:49
how to run Shell commands from GitHub with help of Curl and shortened Git.io URLs [github]
## How to run Shell commands from GitHub with help of Curl and shortened Git.io URLs [github]
# With help of GitHub web UI create public repository (repository name: `REPO`) under
# your GitHub user account (user name: `USER`). This results in Git repository URL
# of `https://github.com/USER/REPO.git`
# Initialize local Git repository and create, commit, and push to the `master` branch
# (or other one) a file (file name: `run`) with shell commands you'd like to run
git init
git remote add origin https://github.com/USER/REPO.git
@mtilson
mtilson / script.sh
Last active April 7, 2020 09:08
how to mirror a GitHub repo [github]
# we are going to fetch all branches and tags from GitHub
# repo 'repo-to-fetch' and push them to GitHub repo 'repo-to-push'
# - 'repo-to-fetch' is supposed to be public available GitHub repo
# under the user account 'public-acc'
# - 'repo-to-push' is newly created GitHub repo the user account
# 'push-acc' has write access to
# create dir and init it as bare git repo
mkdir bare-repo
cd bare-repo