0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

goでprocess substitutionを取る

Posted at

例えばある go のプログラムに

go run main.go <(find .)

のように process substitution をして値を渡したい場合どうやるんだっけと思ったのでメモ

この例の場合 os.Args にファイルディスクリプタが入るのでそこから値を取れます

package main

import (
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	f, _ := os.Open(os.Args[1])
	b, _ := ioutil.ReadAll(f)
	fmt.Println(string(b))
}

別の input と組み合わせる場合は

go run main.go -hoge XXX -ps <(find .)
package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	hoge := flag.String("hoge", "", "")
	ps := flag.String("ps", "", "process substitution")
	flag.Parse()
	fmt.Println(*hoge)
	f, _ := os.Open(*ps)
	b, _ := ioutil.ReadAll(f)
	fmt.Print(string(b))
}

とかして flag と合わせるのが一番楽そうです

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?