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