2018-05-31 15:51:00 +00:00
|
|
|
// run
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2012-02-24 05:24:24 +00:00
|
|
|
// Test string operations including printing.
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
2010-03-30 17:34:57 +00:00
|
|
|
func main() {
|
|
|
|
var c string
|
2008-06-06 21:27:34 +00:00
|
|
|
|
2010-03-30 17:34:57 +00:00
|
|
|
a := `abc`
|
|
|
|
b := `xyz`
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* print a literal */
|
2010-03-30 17:34:57 +00:00
|
|
|
print(`abc`)
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* print a variable */
|
2010-03-30 17:34:57 +00:00
|
|
|
print(b, "-")
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* catenate literals */
|
2010-03-30 17:34:57 +00:00
|
|
|
print(`abc`+`xyz`, "-")
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* catenate variables */
|
2010-03-30 17:34:57 +00:00
|
|
|
print(a+b, "-")
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* compare literals */
|
|
|
|
if `abc` == `xyz` || `abc` != "abc" || `abc` > `xyz` {
|
2010-03-30 17:34:57 +00:00
|
|
|
panic("compare literals")
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* compare variables */
|
|
|
|
if a == b || a != a || a > b {
|
2010-03-30 17:34:57 +00:00
|
|
|
panic("compare variables")
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cat */
|
2010-03-30 17:34:57 +00:00
|
|
|
c = a + b
|
|
|
|
print(c, "-")
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* catequal */
|
2010-03-30 17:34:57 +00:00
|
|
|
c = a
|
|
|
|
c += b
|
|
|
|
print(c, "-")
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* clumsy evaluation */
|
2010-03-30 17:34:57 +00:00
|
|
|
c = b
|
|
|
|
c = a + c
|
|
|
|
print(c, "-")
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* len */
|
|
|
|
if len(c) != 6 {
|
2010-03-30 17:34:57 +00:00
|
|
|
print("len ", len(c))
|
|
|
|
panic("fail")
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* index strings */
|
2010-03-30 17:34:57 +00:00
|
|
|
for i := 0; i < len(c); i = i + 1 {
|
|
|
|
if c[i] != (a + b)[i] {
|
|
|
|
print("index ", i, " ", c[i], " ", (a + b)[i])
|
|
|
|
panic("fail")
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* slice strings */
|
2010-03-30 17:34:57 +00:00
|
|
|
print(c[0:3], c[3:])
|
2008-06-06 21:27:34 +00:00
|
|
|
|
2010-03-30 17:34:57 +00:00
|
|
|
print("\n")
|
2008-06-06 21:27:34 +00:00
|
|
|
|
|
|
|
/* create string with integer constant */
|
2010-03-30 17:34:57 +00:00
|
|
|
c = string('x')
|
2008-06-06 21:27:34 +00:00
|
|
|
if c != "x" {
|
2012-01-19 00:12:24 +00:00
|
|
|
panic("create int " + c)
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create string with integer variable */
|
2010-03-30 17:34:57 +00:00
|
|
|
v := 'x'
|
|
|
|
c = string(v)
|
2008-06-06 21:27:34 +00:00
|
|
|
if c != "x" {
|
2012-01-19 00:12:24 +00:00
|
|
|
panic("create int " + c)
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create string with byte array */
|
2010-03-30 17:34:57 +00:00
|
|
|
var z1 [3]byte
|
|
|
|
z1[0] = 'a'
|
|
|
|
z1[1] = 'b'
|
|
|
|
z1[2] = 'c'
|
2010-06-09 00:51:57 +00:00
|
|
|
c = string(z1[0:])
|
2008-06-06 21:27:34 +00:00
|
|
|
if c != "abc" {
|
2012-01-19 00:12:24 +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 */
|
cgo, goyacc, go/build, html, http, path, path/filepath, testing/quick, test: use rune
Nothing terribly interesting here.
R=golang-dev, bradfitz, gri, r
CC=golang-dev
https://golang.org/cl/5300043
2011-10-26 05:20:02 +00:00
|
|
|
var z2 [3]rune
|
2010-03-30 17:34:57 +00:00
|
|
|
z2[0] = 'a'
|
|
|
|
z2[1] = '\u1234'
|
|
|
|
z2[2] = 'c'
|
2010-06-09 00:51:57 +00:00
|
|
|
c = string(z2[0:])
|
2009-06-04 23:51:47 +00:00
|
|
|
if c != "a\u1234c" {
|
2012-01-19 00:12:24 +00:00
|
|
|
panic("create int array " + c)
|
2009-06-04 23:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create string with byte array pointer */
|
2010-03-30 17:34:57 +00:00
|
|
|
z3 := new([3]byte)
|
|
|
|
z3[0] = 'a'
|
|
|
|
z3[1] = 'b'
|
|
|
|
z3[2] = 'c'
|
2010-06-09 00:51:57 +00:00
|
|
|
c = string(z3[0:])
|
2008-06-06 21:27:34 +00:00
|
|
|
if c != "abc" {
|
2012-01-19 00:12:24 +00:00
|
|
|
panic("create array pointer " + c)
|
2008-06-06 21:27:34 +00:00
|
|
|
}
|
|
|
|
}
|