2008-06-06 21:27:34 +00: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-12 05:07:49 +00:00
|
|
|
print(`abc`);
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* print a variable */
|
2008-08-12 05:07:49 +00:00
|
|
|
print(b, "-");
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* catenate literals */
|
2008-08-12 05:07:49 +00:00
|
|
|
print(`abc` + `xyz`, "-");
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* catenate variables */
|
2008-08-12 05:07:49 +00:00
|
|
|
print(a+b, "-");
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* compare literals */
|
|
|
|
if `abc` == `xyz` || `abc` != "abc" || `abc` > `xyz` {
|
2008-08-12 05:07:49 +00:00
|
|
|
panic("compare literals");
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* compare variables */
|
|
|
|
if a == b || a != a || a > b {
|
2008-08-12 05:07:49 +00:00
|
|
|
panic("compare variables");
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cat */
|
|
|
|
c = a+b;
|
2008-08-12 05:07:49 +00:00
|
|
|
print(c, "-");
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* catequal */
|
|
|
|
c = a;
|
|
|
|
c += b;
|
2008-08-12 05:07:49 +00:00
|
|
|
print(c, "-");
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* clumsy evaluation */
|
|
|
|
c = b;
|
|
|
|
c = a + c;
|
2008-08-12 05:07:49 +00:00
|
|
|
print(c, "-");
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* len */
|
|
|
|
if len(c) != 6 {
|
2008-08-12 05:07:49 +00:00
|
|
|
panic("len ", len(c));
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* index strings */
|
|
|
|
for i:=0; i<len(c); i=i+1 {
|
|
|
|
if c[i] != (a+b)[i] {
|
2008-08-12 05:07:49 +00:00
|
|
|
panic("index ", i, " ", c[i], " ", (a+b)[i]);
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* slice strings */
|
2008-08-12 05:07:49 +00:00
|
|
|
print(c[0:3], c[3:6]);
|
2008-06-06 21:27:34 +00:00
|
|
|
|
2008-08-12 05:07:49 +00:00
|
|
|
print("\n");
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* create string with integer constant */
|
|
|
|
c = string('x');
|
|
|
|
if c != "x" {
|
2008-08-12 05:07:49 +00:00
|
|
|
panic("create int ", c);
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create string with integer variable */
|
|
|
|
v := 'x';
|
|
|
|
c = string(v);
|
|
|
|
if c != "x" {
|
2008-08-12 05:07:49 +00:00
|
|
|
panic("create int ", c);
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create string with byte array */
|
2008-06-21 22:08:04 +00:00
|
|
|
var z1 [3]byte;
|
|
|
|
z1[0] = 'a';
|
|
|
|
z1[1] = 'b';
|
|
|
|
z1[2] = 'c';
|
2009-04-17 06:07:15 +00:00
|
|
|
c = string(&z1);
|
2008-06-06 21:27:34 +00:00
|
|
|
if c != "abc" {
|
2009-06-04 23:51:47 +00:00
|
|
|
panic("create byte array ", c);
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
|
2009-06-04 23:51:47 +00:00
|
|
|
/* create string with int array */
|
|
|
|
var z2 [3]int;
|
2008-06-21 22:08:04 +00:00
|
|
|
z2[0] = 'a';
|
2009-06-04 23:51:47 +00:00
|
|
|
z2[1] = '\u1234';
|
2008-06-21 22:08:04 +00:00
|
|
|
z2[2] = 'c';
|
2009-06-04 23:51:47 +00:00
|
|
|
c = string(&z2);
|
|
|
|
if c != "a\u1234c" {
|
|
|
|
panic("create int array ", c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create string with byte array pointer */
|
|
|
|
z3 := new([3]byte);
|
|
|
|
z3[0] = 'a';
|
|
|
|
z3[1] = 'b';
|
|
|
|
z3[2] = 'c';
|
|
|
|
c = string(z3);
|
2008-06-06 21:27:34 +00:00
|
|
|
if c != "abc" {
|
2008-08-12 05:07:49 +00:00
|
|
|
panic("create array pointer ", c);
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
}
|