| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | // $G $D/$F.go && $L $F.$A && ./$A.out | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Copyright 2009 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. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func | 
					
						
							|  |  |  | main() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	var c string; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	a := `abc`; | 
					
						
							|  |  |  | 	b := `xyz`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* print a literal */ | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 	print(`abc`); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* print a variable */ | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 	print(b, "-"); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* catenate literals */ | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 	print(`abc` + `xyz`, "-"); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* catenate variables */ | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 	print(a+b, "-"); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* compare literals */ | 
					
						
							|  |  |  | 	if `abc` == `xyz` || `abc` != "abc" || `abc` > `xyz` { | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 		panic("compare literals"); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* compare variables */ | 
					
						
							|  |  |  | 	if a == b || a != a || a > b { | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 		panic("compare variables"); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* cat */ | 
					
						
							|  |  |  | 	c = a+b; | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 	print(c, "-"); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* catequal */ | 
					
						
							|  |  |  | 	c = a; | 
					
						
							|  |  |  | 	c += b; | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 	print(c, "-"); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* clumsy evaluation */ | 
					
						
							|  |  |  | 	c = b; | 
					
						
							|  |  |  | 	c = a + c; | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 	print(c, "-"); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* len */ | 
					
						
							|  |  |  | 	if len(c) != 6 { | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 		panic("len ", len(c)); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* index strings */ | 
					
						
							|  |  |  | 	for i:=0; i<len(c); i=i+1 { | 
					
						
							|  |  |  | 		if c[i] != (a+b)[i] { | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 			panic("index ", i, " ", c[i], " ", (a+b)[i]); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* slice strings */ | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 	print(c[0:3], c[3:6]); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 	print("\n"); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* create string with integer constant */ | 
					
						
							|  |  |  | 	c = string('x'); | 
					
						
							|  |  |  | 	if c != "x" { | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 		panic("create int ", c); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* create string with integer variable */ | 
					
						
							|  |  |  | 	v := 'x'; | 
					
						
							|  |  |  | 	c = string(v); | 
					
						
							|  |  |  | 	if c != "x" { | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 		panic("create int ", c); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* create string with byte array */ | 
					
						
							| 
									
										
										
										
											2008-06-21 15:08:04 -07:00
										 |  |  | 	var z1 [3]byte; | 
					
						
							|  |  |  | 	z1[0] = 'a'; | 
					
						
							|  |  |  | 	z1[1] = 'b'; | 
					
						
							|  |  |  | 	z1[2] = 'c'; | 
					
						
							|  |  |  | 	c = string(z1); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 	if c != "abc" { | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 		panic("create array ", c); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* create string with byte array pointer */ | 
					
						
							| 
									
										
										
										
											2009-01-06 15:19:02 -08:00
										 |  |  | 	z2 := new([3]byte); | 
					
						
							| 
									
										
										
										
											2008-06-21 15:08:04 -07:00
										 |  |  | 	z2[0] = 'a'; | 
					
						
							|  |  |  | 	z2[1] = 'b'; | 
					
						
							|  |  |  | 	z2[2] = 'c'; | 
					
						
							| 
									
										
										
										
											2008-12-20 13:38:29 -08:00
										 |  |  | 	c = string(*z2); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 	if c != "abc" { | 
					
						
							| 
									
										
										
										
											2008-08-11 22:07:49 -07:00
										 |  |  | 		panic("create array pointer ", c); | 
					
						
							| 
									
										
										
										
											2008-06-06 14:27:34 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |