本文整理汇总了Golang中github.com/mbenkmann/golib/bytes.Buffer.WriteString方法的典型用法代码示例。如果您正苦于以下问题:Golang Buffer.WriteString方法的具体用法?Golang Buffer.WriteString怎么用?Golang Buffer.WriteString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/mbenkmann/golib/bytes.Buffer
的用法示例。
在下文中一共展示了Buffer.WriteString方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
if len(os.Args) != 3 && len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "USAGE: %v", USAGE)
os.Exit(0)
}
var input bytes.Buffer
defer input.Reset()
if len(os.Args) == 3 {
input.WriteString(os.Args[2])
} else {
buf, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
}
input.Write(buf)
}
security.GosaEncryptBuffer(&input, os.Args[1])
fmt.Fprintln(os.Stdout, input.String())
}
示例2: Send_clmsg_save_fai_log
// Executes program and reads from its standard output log files to transfer to
// the target server. See fai-savelog-hook in the manual.
func Send_clmsg_save_fai_log(target string, program string) {
var buffy bytes.Buffer
defer buffy.Reset()
clientpackageskey := config.ModuleKey["[ClientPackages]"]
// If [ClientPackages]/key missing, take the last key in the list
// (We don't take the 1st because that would be "dummy-key").
if clientpackageskey == "" {
clientpackageskey = config.ModuleKeys[len(config.ModuleKeys)-1]
}
util.Log(1, "INFO! Launching fai-savelog-hook %v", program)
start := time.Now()
env := config.HookEnvironment()
cmd := exec.Command(program)
cmd.Env = append(env, os.Environ()...)
out, err := cmd.StdoutPipe()
if err != nil {
util.Log(0, "ERROR! Could not get stdout pipe for %v: %v", program, err)
return
}
defer out.Close()
in, err := cmd.StdinPipe()
if err != nil {
util.Log(0, "ERROR! Could not get stdin pipe for %v: %v", program, err)
return
}
defer in.Close()
err = cmd.Start()
if err != nil {
util.Log(0, "ERROR! Could not launch %v: %v", program, err)
return
}
buffy.WriteString("<xml><header>CLMSG_save_fai_log</header><source>")
buffy.WriteString(config.ServerSourceAddress)
buffy.WriteString("</source>")
buffy.WriteString("<target>")
buffy.WriteString(target)
buffy.WriteString("</target>")
buffy.WriteString("<macaddress>")
buffy.WriteString(config.MAC)
buffy.WriteString("</macaddress>")
buffy.WriteString("<CLMSG_save_fai_log>")
reader := bufio.NewReader(out)
fai_action := ""
for {
line, err := reader.ReadString('\n')
if err != nil {
util.Log(0, "ERROR! Error reading stdout from %v: %v", program, err)
return
}
line = strings.TrimSpace(line)
if line == "install" || line == "softupdate" {
fai_action = line
break
}
buffy.WriteString(line)
}
util.Log(1, "INFO! Received %v bytes in %v from fai-savelog-hook", buffy.Len(), time.Since(start))
buffy.WriteString("</CLMSG_save_fai_log>")
buffy.WriteString("<fai_action>")
buffy.WriteString(fai_action)
buffy.WriteString("</fai_action>")
buffy.WriteString("</xml>")
util.Log(1, "INFO! Sending %v bytes of log files to %v", buffy.Len(), target)
security.SendLnTo(target, buffy.String(), clientpackageskey, false)
in.Write([]byte{'\n'}) // notify hook that transfer is complete
}
示例3: testBuffer
func testBuffer() {
var b bytes.Buffer
check(b.String(), "") // String() on fresh variable
b.Reset() // Reset() on fresh variable
check(b.String(), "") // String() after Reset()
b.Reset() // Reset() after Reset()
check(b.String(), "")
check(b.Len(), 0)
// same tests as above with pointer
b2 := &bytes.Buffer{}
check(b2.String(), "")
b2.Reset()
check(b2.String(), "")
b2.Reset()
check(b2.String(), "")
check(b2.Len(), 0)
b2.WriteString("Dies ist ein Test!")
check(b2.String(), "Dies ist ein Test!")
check(b2.Len(), 18)
n, err := b.Write(nil)
check(n, 0)
check(err, nil)
check(b.String(), "")
n, err = b.Write([]byte{})
check(n, 0)
check(err, nil)
check(b.String(), "")
check(b.Pointer(), nil)
check(b.Capacity(), 0)
check(b.Len(), 0)
func() {
defer func() {
check(recover(), bytes.ErrTooLarge)
}()
b.Grow(-1)
}()
n, err = b.Write([]byte{'a'})
check(n, 1)
check(err, nil)
check(b.String(), "a")
check(b.Capacity() >= 1, true)
check(b.Len(), 1)
check(b.Pointer() != nil, true)
check(b.Grow(11), 1)
check(b.Capacity() >= 12, true)
c := b.Capacity()
p := b.Pointer()
check(b.Grow(11), 1) // should not cause actual growth
check(b.Pointer(), p)
check(b.Capacity(), c)
check(b.Len(), 1)
((*[2]byte)(b.Pointer()))[1] = 'z'
check(b.Contains("z"), false)
n, err = b.WriteString("Hallo")
check(n, 5)
check(err, nil)
check(b.String(), "aHallo")
check(b.Pointer(), p)
check(b.Capacity(), c)
check(b.Len(), 6)
b.Reset()
check(b.String(), "")
check(b.Pointer(), nil)
check(b.Capacity(), 0)
check(b.Contains(""), true)
check(b.Contains("a"), false)
b.WriteString("Hallo")
b.WriteByte(' ')
b.Write([]byte{'d', 'i', 'e', 's'})
b.WriteByte(' ')
b.WriteString("ist ")
b.WriteString("ein ")
b.Write([]byte("Test"))
check(b.String(), "Hallo dies ist ein Test")
check(b.Contains("Hallo dies ist ein Test"), true)
check(b.Contains("Test"), true)
check(b.Contains("Hallo"), true)
check(b.Contains("allo"), true)
check(b.Contains(""), true)
check(b.Split(" "), []string{"Hallo", "dies", "ist", "ein", "Test"})
check(b.Split("X"), []string{"Hallo dies ist ein Test"})
check(b.Split("Hallo dies ist ein Test"), []string{"", ""})
check(b.Split("H"), []string{"", "allo dies ist ein Test"})
check(b.Split("Test"), []string{"Hallo dies ist ein ", ""})
check(b.Split("es"), []string{"Hallo di", " ist ein T", "t"})
b.Reset()
b.WriteString(" \n\t Hallo \t\v\n")
check(b.Len(), 15)
//.........这里部分代码省略.........