| 
									
										
										
										
											2012-01-18 09:40:50 -08:00
										 |  |  | // Copyright 2011 The Go Authors. All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-16 09:43:58 +11:00
										 |  |  | package bytes_test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2012-10-10 11:15:41 +11:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2011-12-16 09:43:58 +11:00
										 |  |  | 	"encoding/base64" | 
					
						
							| 
									
										
										
										
											2012-10-10 11:15:41 +11:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2011-12-16 09:43:58 +11:00
										 |  |  | 	"io" | 
					
						
							|  |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2013-01-07 09:59:37 +11:00
										 |  |  | 	"sort" | 
					
						
							| 
									
										
										
										
											2011-12-16 09:43:58 +11:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ExampleBuffer() { | 
					
						
							| 
									
										
										
										
											2012-10-10 11:15:41 +11:00
										 |  |  | 	var b bytes.Buffer // A Buffer needs no initialization. | 
					
						
							| 
									
										
										
										
											2011-12-16 09:43:58 +11:00
										 |  |  | 	b.Write([]byte("Hello ")) | 
					
						
							| 
									
										
										
										
											2012-10-10 11:15:41 +11:00
										 |  |  | 	fmt.Fprintf(&b, "world!") | 
					
						
							| 
									
										
										
										
											2011-12-16 09:43:58 +11:00
										 |  |  | 	b.WriteTo(os.Stdout) | 
					
						
							| 
									
										
										
										
											2012-02-16 11:50:28 +11:00
										 |  |  | 	// Output: Hello world! | 
					
						
							| 
									
										
										
										
											2011-12-16 09:43:58 +11:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ExampleBuffer_reader() { | 
					
						
							|  |  |  | 	// A Buffer can turn a string or a []byte into an io.Reader. | 
					
						
							| 
									
										
										
										
											2012-10-10 11:15:41 +11:00
										 |  |  | 	buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==") | 
					
						
							| 
									
										
										
										
											2011-12-16 09:43:58 +11:00
										 |  |  | 	dec := base64.NewDecoder(base64.StdEncoding, buf) | 
					
						
							|  |  |  | 	io.Copy(os.Stdout, dec) | 
					
						
							| 
									
										
										
										
											2012-02-16 11:50:28 +11:00
										 |  |  | 	// Output: Gophers rule! | 
					
						
							| 
									
										
										
										
											2011-12-16 09:43:58 +11:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-01-07 09:59:37 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | func ExampleCompare() { | 
					
						
							|  |  |  | 	// Interpret Compare's result by comparing it to zero. | 
					
						
							|  |  |  | 	var a, b []byte | 
					
						
							|  |  |  | 	if bytes.Compare(a, b) < 0 { | 
					
						
							|  |  |  | 		// a less b | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if bytes.Compare(a, b) <= 0 { | 
					
						
							|  |  |  | 		// a less or equal b | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if bytes.Compare(a, b) > 0 { | 
					
						
							|  |  |  | 		// a greater b | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if bytes.Compare(a, b) >= 0 { | 
					
						
							|  |  |  | 		// a greater or equal b | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Prefer Equal to Compare for equality comparisons. | 
					
						
							|  |  |  | 	if bytes.Equal(a, b) { | 
					
						
							|  |  |  | 		// a equal b | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if !bytes.Equal(a, b) { | 
					
						
							|  |  |  | 		// a not equal b | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func ExampleCompare_search() { | 
					
						
							|  |  |  | 	// Binary search to find a matching byte slice. | 
					
						
							|  |  |  | 	var needle []byte | 
					
						
							|  |  |  | 	var haystack [][]byte // Assume sorted | 
					
						
							|  |  |  | 	i := sort.Search(len(haystack), func(i int) bool { | 
					
						
							| 
									
										
										
										
											2013-01-06 22:43:32 -05:00
										 |  |  | 		// Return haystack[i] >= needle. | 
					
						
							|  |  |  | 		return bytes.Compare(haystack[i], needle) >= 0 | 
					
						
							| 
									
										
										
										
											2013-01-07 09:59:37 +11:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2013-01-06 22:43:32 -05:00
										 |  |  | 	if i < len(haystack) && bytes.Equal(haystack[i], needle) { | 
					
						
							| 
									
										
										
										
											2013-01-07 09:59:37 +11:00
										 |  |  | 		// Found it! | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |