Skip to content

Instantly share code, notes, and snippets.

@ymgyt
ymgyt / binominal_distribution.rs
Created September 13, 2020 07:29
Draw a binomial distribution as a svg graph
use itertools::Itertools;
use plotlib::page::Page;
use plotlib::repr::Plot;
use plotlib::style::{LineStyle, PointMarker, PointStyle};
use plotlib::view::ContinuousView;
fn main() {
let p: f64 = 0.5;
let q = 1. - p;
let n = 20;
@ymgyt
ymgyt / console_coloring.go
Created March 8, 2018 11:51
coloring console output example
package main
import "fmt"
var colorRed = string([]byte{27, 91, 57, 55, 59, 51, 49, 59, 49, 109})
var colorReset = string([]byte{27, 91, 48, 109})
func main() {
fmt.Printf("%s messag%s normal\n", colorRed, colorReset)
}
@ymgyt
ymgyt / builder_build.go
Created March 8, 2018 11:36
exec.Command example
func (b *builder) Build() error {
args := append([]string{"go", "build", "-o", filepath.Join(b.wd, b.binary)}, b.buildArgs...)
var command *exec.Cmd
if b.useGodep {
args = append([]string{"godep"}, args...)
}
command = exec.Command(args[0], args[1:]...)
command.Dir = b.dir
@ymgyt
ymgyt / file1.txt
Last active November 24, 2017 13:54
Goからlocalのtest用DB(MySQL)をdockerで起動する ref: https://qiita.com/YmgchiYt/items/cc97142614f5b61a69e9
# containerは存在しない
docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 起動
go run main.go
2017-11-23T02:30:38.881+0900 INFO workspace/main.go:52 local_docker_db {"status": "starting"}
2017-11-23T02:30:38.934+0900 DEBUG workspace/main.go:77 local_docker_db {"container_info": "not_found", "container": "myproject-mysql-db"}
2017-11-23T02:30:39.428+0900 DEBUG workspace/main.go:107 local_docker_db {"exec docker cmd": "docker container run --detach --name myproject-mysql-db --publish 3307:3306 --mount type=bind,source=/tmp/docker_db,target=/var/lib/mysql --env MYSQL_USER=gopher --env MYSQL_PASSWORD=golangorgohome --env MYSQL_INITDB_SKIP_TZINFO=yes --env MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql:5.6", "stdout": "4a7336c9364b14df0813463161335d8690d9c6e777f5c631bb406b5f55d2e181\n", "stderr": ""}
@ymgyt
ymgyt / file0.go
Last active November 24, 2017 13:55
Goでモンティ・ホール問題を試してみる ref: https://qiita.com/YmgchiYt/items/1ce7d39309de5406953e
func game(change bool) int {
choice := rand.Intn(3)
prize := rand.Intn(3)
if change {
var opened int
for {
opened = rand.Intn(3)
if opened != prize && opened != choice {
break
@ymgyt
ymgyt / context.go
Created October 4, 2017 15:53 — forked from iamralch/context.go
An example that illustrates how to work with https://godoc.org/golang.org/x/net/context
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
"golang.org/x/net/context"
@ymgyt
ymgyt / safebuffer.go
Created October 4, 2017 10:25 — forked from arkan/safebuffer.go
Golang: Buffer is a goroutine safe bytes.Buffer
package safebuffer
import (
"bytes"
"sync"
)
// Buffer is a goroutine safe bytes.Buffer
type Buffer struct {
buffer bytes.Buffer
@ymgyt
ymgyt / safebuffer.go
Created October 4, 2017 10:25 — forked from arkan/safebuffer.go
Golang: Buffer is a goroutine safe bytes.Buffer
package safebuffer
import (
"bytes"
"sync"
)
// Buffer is a goroutine safe bytes.Buffer
type Buffer struct {
buffer bytes.Buffer
@ymgyt
ymgyt / check.go
Created October 4, 2017 05:44 — forked from mattes/check.go
Check if file or directory exists in Golang
if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err) {
// path/to/whatever does not exist
}
if _, err := os.Stat("/path/to/whatever"); err == nil {
// path/to/whatever exists
}
@ymgyt
ymgyt / subcommand.go
Created September 28, 2017 08:34 — forked from iamralch/subcommand.go
flag package: subcommand example
package main
import (
"flag"
"fmt"
"os"
)
func main() {
askCommand := flag.NewFlagSet("ask", flag.ExitOnError)