Confussed In Int

well if compile this code then it not shows error even x=89.0 with float value


1 Like

But why it should? According to the documentation, conversion section:

A constant value x can be converted to type T if x is representable by a value of T .

89.0 it’s the same as just 89.

P.S. Try to provide copyable code instead of image.

1 Like

package main

import (

func main() {
var x int16 = 32668
y := 89.9

fmt.Println(x, y)
fmt.Printf("%T\t%T\n", x, y)


1 Like

Sorry, but I still do not understand what exactly unclear for you. First code is totally ok, 89.0 is convertable to int. Second code also is totally ok, since 89.9 is float. Try to change 89.0 in first example to something unconvertable (i.e. 89.9) and you get error.

1 Like

Hi, @M_A_D_H_U_R,

See the Go language specification here:

“untyped” constants such as 89.0, like in your example, do have a default type of float32 or float64 when no type is explicitly specified:

However: “untyped” constants are just constants. If you assign the constant 89.0 to a variable of type int explicitly, there is no error because the compiler “knows” how to treat that constant value as an integer without any ambiguity and/or loss of precision.

If you change 89.0 to 89.1:
You will see that suddenly, the type is float64 (or perhaps float32 on some architectures). Now that there is an actual decimal value, the compiler “knows” it cannot be an integer value and therefore must be a floating point value.

When you explicitly state the type:
There is still no problem.

If, however, you try this:
You will get an error because you have defined the explicit type of the variable x to be int but you have assigned a constant value that the compiler “knows” cannot be represented as an integer and therefore you get the error:

./prog.go:8:14: constant 89.1 truncated to integer

Go’s handling of constants is different from those of other C-like languages such as C, C++, C# and potentially Java (I do not know Java). In languages such as C#, special characters at the end of numeric literals signal the constant’s type.

For example, in C#:

  • 1 is a System.Int32
  • 1L is a System.Int64
  • 1F is a System.Single
  • 1D is a System.Double
  • 1M is a System.Decimal


In Go, the characters at the end of numeric literals are not necessary because constants work differently. For example, you can write this in Go:

But in a language like C# where constant types work differently, you’d have to do something like:

using System;

public struct MyString
    private readonly string value;

    public MyString(string value)
        this.value = value;

    public static implicit operator MyString(string value)
        return new MyString(value);

public class Program
    public static void Main(string[] args)
        var v = MyFunc("Hello, world")

    public static string MyFunc(MyString v)
        return String.Format("{0}: {1}", v.GetType(), v);

Disclaimer: I just wrote this in the MarkDown editor quickly and likely have made mistakes that make the example above uncompileable.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.