Skip to content

Instantly share code, notes, and snippets.

@ymgyt
ymgyt / binominal_distribution.rs
Created Sep 13, 2020
Draw a binomial distribution as a svg graph
View binominal_distribution.rs
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 Mar 8, 2018
coloring console output example
View console_coloring.go
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 Mar 8, 2018
exec.Command example
View builder_build.go
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 Nov 24, 2017
Goからlocalのtest用DB(MySQL)をdockerで起動する ref: https://qiita.com/YmgchiYt/items/cc97142614f5b61a69e9
View file1.txt
# 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 Nov 24, 2017
Goでモンティ・ホール問題を試してみる ref: https://qiita.com/YmgchiYt/items/1ce7d39309de5406953e
View file0.go
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 Oct 4, 2017 — forked from iamralch/context.go
An example that illustrates how to work with https://godoc.org/golang.org/x/net/context
View context.go
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
"golang.org/x/net/context"
@ymgyt
ymgyt / safebuffer.go
Created Oct 4, 2017 — forked from arkan/safebuffer.go
Golang: Buffer is a goroutine safe bytes.Buffer
View safebuffer.go
package safebuffer
import (
"bytes"
"sync"
)
// Buffer is a goroutine safe bytes.Buffer
type Buffer struct {
buffer bytes.Buffer
@ymgyt
ymgyt / safebuffer.go
Created Oct 4, 2017 — forked from arkan/safebuffer.go
Golang: Buffer is a goroutine safe bytes.Buffer
View safebuffer.go
package safebuffer
import (
"bytes"
"sync"
)
// Buffer is a goroutine safe bytes.Buffer
type Buffer struct {
buffer bytes.Buffer
@ymgyt
ymgyt / check.go
Created Oct 4, 2017 — forked from mattes/check.go
Check if file or directory exists in Golang
View check.go
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 Sep 28, 2017 — forked from iamralch/subcommand.go
flag package: subcommand example
View subcommand.go
package main
import (
"flag"
"fmt"
"os"
)
func main() {
askCommand := flag.NewFlagSet("ask", flag.ExitOnError)