Russ Cox | 2b1c9b4 | 2012-02-16 23:49:30 -0500 | [diff] [blame] | 1 | // errorcheck |
Rob Pike | 3aecf2e | 2008-08-13 12:15:24 -0700 | [diff] [blame] | 2 | |
| 3 | // Copyright 2009 The Go Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | |
| 7 | package main |
| 8 | |
| 9 | const ( |
| 10 | c3div2 = 3/2; |
| 11 | f3div2 = 3./2.; |
| 12 | ) |
| 13 | |
| 14 | func assert(t bool, s string) { |
| 15 | if !t { |
| 16 | panic(s) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func main() { |
| 21 | var i int; |
| 22 | var f float64; |
| 23 | |
| 24 | assert(c3div2 == 1, "3/2"); |
| 25 | assert(f3div2 == 1.5, "3/2"); |
| 26 | |
| 27 | i = c3div2; |
| 28 | assert(i == c3div2, "i == c3div2"); |
| 29 | |
| 30 | f = c3div2; |
| 31 | assert(f == c3div2, "f == c3div2"); |
| 32 | |
| 33 | f = f3div2; |
| 34 | assert(f == f3div2, "f == f3div2"); |
| 35 | |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 36 | i = f3div2; // ERROR "truncate" |
Rob Pike | 3aecf2e | 2008-08-13 12:15:24 -0700 | [diff] [blame] | 37 | assert(i == c3div2, "i == c3div2 from f3div2"); |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 38 | assert(i != f3div2, "i != f3div2"); // ERROR "truncate" |
Robert Griesemer | a9af184 | 2008-08-20 15:43:12 -0700 | [diff] [blame] | 39 | |
| 40 | const g float64 = 1.0; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 41 | i = g; // ERROR "convert|incompatible|cannot" |
Robert Griesemer | a9af184 | 2008-08-20 15:43:12 -0700 | [diff] [blame] | 42 | |
| 43 | const h float64 = 3.14; |
Russ Cox | 9dc22b6 | 2009-08-03 11:58:52 -0700 | [diff] [blame] | 44 | i = h; // ERROR "convert|incompatible|cannot" |
Russ Cox | 8f194bf | 2009-03-12 19:04:38 -0700 | [diff] [blame] | 45 | i = int(h); // ERROR "truncate" |
Rob Pike | 3aecf2e | 2008-08-13 12:15:24 -0700 | [diff] [blame] | 46 | } |