Skip to content

Instantly share code, notes, and snippets.

@christophberger
christophberger / futures.go
Last active November 28, 2022 07:42
Code from appliedgo.net/futures
View futures.go
package main
import (
"fmt"
"time"
)
func main() {
c := make(chan int)
@christophberger
christophberger / append_question.go
Last active October 1, 2019 05:35
I don't know the theory;please give me a answer!!thank you
View append_question.go
package main
import (
"fmt"
)
func main() {
x := make([]int, 1, 10)
var a []int
View what is the reflection and why it's cool and dangerous at the same time
The Package reflect in the Go standard library implements run-time reflection, allowing a program to manipulate objects with arbitrary types.
We can build with the reflection package a kind of generic functionality. Because this package give us the possibility to determine the meta information of a function and it's input and output arguments while runtime.
Be careful when you start to use the reflection package it's hard to debug run time errors. For more information about reflection, I can recommend the following article about reflection.
See "The Laws of Reflection" for an introduction to reflection in Go: https://golang.org/doc/articles/laws_of_reflection.html
If you want see reflection in action below is a example:
@christophberger
christophberger / tui4main.go
Created April 4, 2017 09:38
Code snippet from github.com/appliedgo/tui: main
View tui4main.go
func main() {
if len(os.Args) <= 1 {
log.Println("Usage: go run tui.go [termui|gocui]")
return
}
if os.Args[1] == "termui" {
runTermui()
return
}
if os.Args[1] == "gocui" {
@christophberger
christophberger / tui3gocui.go
Last active April 4, 2017 09:39
Code snippet from github.com/appliedgo/tui: gocui
View tui3gocui.go
func runGocui() {
// Create a new GUI.
g, err := c.NewGui(c.OutputNormal)
if err != nil {
log.Println("Failed to create a GUI:", err)
return
}
defer g.Close()
// Activate the cursor for the current view.
@christophberger
christophberger / tui2termui.go
Last active April 4, 2017 09:39
Code snippet from github.com/appliedgo/tui: termui
View tui2termui.go
func runTermui() {
// Initialize termui.
err := t.Init()
if err != nil {
log.Fatalln("Cannot initialize termui")
}
// `termui` needs some cleanup when terminating.
defer t.Close()
// Get the height of the terminal.
@christophberger
christophberger / tui1start.go
Last active April 4, 2017 09:38
Code snippet from github.com/appliedgo/tui: start
View tui1start.go
package main
// Under normal circumstances, importing two UI libraries at the
// same time is probably not a good idea, as each has its own event
// loop. This demo code takes care of using only one of these
// libraries at a time.
import (
"fmt"
"log"
"os"
@christophberger
christophberger / tui_code_1_start.go
Last active April 4, 2017 09:30
Code from github.com/appliedgo/tui
View tui_code_1_start.go
package main
// Under normal circumstances, importing two UI libraries at the
// same time is probably not a good idea, as each has its own event
// loop. This demo code takes care of using only one of these
// libraries at a time.
import (
"fmt"
"log"
"os"
@christophberger
christophberger / container.go
Created October 30, 2016 15:41
A container in less than 60 lines of Go
View container.go
package main
import (
"fmt"
"os"
"os/exec"
"syscall"
)
func main() {
View keybase.md

Keybase proof

I hereby claim:

  • I am christophberger on github.
  • I am christophberger (https://keybase.io/christophberger) on keybase.
  • I have a public key ASC8b-gcdQzzyAjieAUZNehGbw2z1Fd2V2xp8SduLfHYxAo

To claim this, I am signing this object: