當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Buffer.WriteString方法代碼示例

本文整理匯總了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())
}
開發者ID:chrlutz,項目名稱:limux-gosa,代碼行數:21,代碼來源:encrypt.go

示例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
}
開發者ID:chrlutz,項目名稱:limux-gosa,代碼行數:81,代碼來源:clmsg_save_fai_log.go

示例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)
//.........這裏部分代碼省略.........
開發者ID:chrlutz,項目名稱:limux-gosa,代碼行數:101,代碼來源:bytes-test.go


注:本文中的github.com/mbenkmann/golib/bytes.Buffer.WriteString方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。