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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
) | |
func handler(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("ok")) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net" | |
) | |
func handle(conn net.Conn) { | |
defer conn.Close() | |
var readBuf = make([]byte, 512) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "net/http" | |
var counterChan chan int | |
func counter() { | |
for i := 0; ; i++ { counterChan <- i } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://play.golang.org/p/1hGHFwjlwud | |
package main | |
import ( | |
"bytes" | |
"fmt" | |
"io" | |
) | |
func main() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type InterfaceType interface { | |
MethodReceiverIsPtr() | |
MethodReceiverIsValue() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://play.golang.org/p/Nolp4FTyp9I | |
package main | |
import "fmt" | |
type T struct {} | |
func (*T) String() string { // receiver parameter has type *T | |
return "" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |