Got an error when by mistak declare variable name same as package name ?why

  1. package main
  2. import (
  3. "fmt"
    
  4. "runtime"    
    
  5. )
  6. func main() {
  7. fmt.Print("Go runs on ")
    
  8. fmt := 1
    
  9. switch os := runtime.GOOS; os {
    
  10. case "darwin":
    
  11.     fmt.Println("OS X.")
    
  12. case "linux":
    
  13.     fmt.Println("Linux.")
    
  14. default:
    
  15.     // freebsd, openbsd,
    
  16.     // plan9, windows...
    
  17.     fmt.Printf("%s.\n", os)
    
  18. }
    
  19. }

Error are
prog.go:12: fmt.Println undefined (type int has no field or method Println)
prog.go:14: fmt.Println undefined (type int has no field or method Println)
prog.go:18: fmt.Printf undefined (type int has no field or method Printf)

when i initialize fmt := 1 in line 9

You’re declaring a variable fmt of type int which shadows the package fmt. From that point on, in that scope, fmt refers to that variable which does not have Println and so on methods.

ok.

is it possible to access fmt package and declare fmt variable like getting global variable using :: operator in c++

#include
using namespace std;
int i = 10;
int main()
{
int i;
cout<<::i<<endl;
}

output is:
10

No.

I’m not if this is something you’ll want to do, but if you are adamant about naming your variable fmt, then you can always import the fmt package under a different name such as format and call it’s functions with the new name. For example:

package main

import format "fmt"

func main() {
	fmt := 1

	format.Println(fmt)
}

My point is
In C programming we can declare any number of variables with different names it is not depend on any header file inclusion
But to delcare variable in go lang we need to take care of package name in such way that variable should not shadow the package
Ex1:
there is package " import “flag” " so we can not take local variable of “var flag bool” i have choose different name if i want to

Ex:2
there is package " import “time” " so we can not take local variable of “var time int32” i have choose different name
and if there are tones of packages and very big code this will cause problem during compile time

Then in such cases i should have to follow some naming conventions

@DineshD, I don’t think very often you will come across name collisions in Go as an issue like you are describing. For there to be a problem with name collisions, you would really have to import a very large number of packages, even though in my above post I described how it can be avoided.

However, here is another method you could use to avoid a name collision, even though I hardly think it would actually be an issue if variables are being named appropriately.

package main

import "fmt"

func main() {
	{
		write := fmt.Println
		fmt := 5
		write(fmt)
	}
	fmt.Println("different scope")
}
1 Like

I don’t see that there is a difference necessarily. Unless you need to access the package in question, you don’t care if it’s shadowed. That’s the beauty of shadowing. If you do need to access both you need to care, much like you need to in C, and in practice you’ll be unlikely to call a variable fmt while also wanting to use the package fmt.

1 Like

Go is a Go-1, not a Go-2.

(Lisp joke.)

1 Like

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