LoginSignup
0
1

More than 5 years have passed since last update.

【メモ】Go言語でptyを使ってみる

Last updated at Posted at 2018-09-15

使用するライブラリ

github.com/kr/pty

Hello World

package main

import (
    "fmt"
    "github.com/kr/pty"
)

func main() {
    // 仮想端末をオープン
    ptmx, tty, err := pty.Open()
    if err != nil {
        panic(err)
    }
    defer ptmx.Close()
    defer tty.Close()

    // 仮想端末に対して書き込み
    buf := make([]byte, 1024)
    ptmx.Write([]byte("Hello World!"))

    // 読み込んで表示
    n, err := ptmx.Read(buf)
    fmt.Println(string(buf[:n]))
    if err != nil {
        panic(err)
    }
}

結果

Hello World!
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