Re: "imported and not used" should be a warning

1,219 views
Skip to first unread message
Message has been deleted

John Asmuth

unread,
Dec 9, 2009, 7:39:13 PM12/9/09
to golang-nuts
_ is not for suppressing the error, it is to use invoke the package's
initialization without gaining its namespace.

You can't import a package without using it because doing so bloats
the amount of things that the compiler needs to deal with.

On Dec 9, 7:20 pm, inspector_jouve <kaushan...@gmail.com> wrote:
> I suggest that "imported and not used" error should be a warning
> instead. I know you can use _ to suppress the error, but it's not
> convenient anyway, you can as well just comment this import out -so
> the feature of import _ "mypkg" seems basically useless.
Message has been deleted

ziyu_huang

unread,
Dec 9, 2009, 9:04:30 PM12/9/09
to golang-nuts
I hope compiler have error check level, too.
I know it should be bad when you import or declare something and don't
use it.
But something I just try out the code, and maybe just try to comment
out code.
I'd like to see it report just a WARNING sometimes.

Russ Cox

unread,
Dec 9, 2009, 9:17:44 PM12/9/09
to ziyu_huang, golang-nuts
On Wed, Dec 9, 2009 at 18:04, ziyu_huang <ziyu4...@gmail.com> wrote:
> I hope compiler have error check level, too.

http://groups.google.com/group/golang-nuts/browse_thread/thread/a1b6e1f4093e2f34/9c447a46a3a45d76?q="worth+fixing"
Message has been deleted

Russ Cox

unread,
Dec 9, 2009, 11:04:16 PM12/9/09
to inspector_jouve, golang-nuts
On Wed, Dec 9, 2009 at 20:00, inspector_jouve <kaush...@gmail.com> wrote:
> Until you start deprecating things. Then you will have to introduce
> warnings. A thing worth fixing, but not an error. Is this a correct
> assessment?

Nope. That's what gofmt is for.
(See the mail about the most recent release;
we didn't deprecate bytes.Copy; we removed it.)

Russ
Message has been deleted

Esko Luontola

unread,
Dec 13, 2009, 2:37:23 PM12/13/09
to golang-nuts
A typical use case where "imported and not used" is annoying, is when
during development you every now and then want to print some debug
statements. The workflow goes like this:

1. You don't understand exactly why some test case is failing, so you
add a fmt.Println().
2. You try to compile and run tests, but it doesn't compile, because
the import was missing.
3. You edit the file and add the missing import.
4. Compile and run tests.
5. You figure out what the problem was, so you fix it. Now the Println
is not anymore needed, so you remove also it.
6. You try to compile and run tests, but it doesn't compile, because
there is an unused import.
7. You edit the file and comment out import.
8. Compile and run tests.

This causes lots of unproductive errors and changes to the code.

My current workaround is to have the following unused global variable
in all files where I'm actively developing, just so that I could more
quickly add and remove the Println statements, without having to
always change the imports:

var x = fmt.Sprintf("keep 'fmt' import during debugging"); // TODO:
remove

Kaito Kumashiro

unread,
Dec 13, 2009, 3:17:45 PM12/13/09
to golang-nuts
On Sun, Dec 13, 2009 at 8:37 PM, Esko Luontola <esko.l...@gmail.com> wrote:
> A typical use case where "imported and not used" is annoying, is when
> during development you every now and then want to print some debug
> statements. The workflow goes like this:
I think all the "...and not used" should be warnings, not errors (just
like in gcc).
The same problem is with unused variables. When I type code, put some
"var foo int" and compile just to check something but before I use
this variable,
I have to comment out the declaration, because compiler refuses to build the
object.


P.S. I'm sorry Esko, I've sent the the previous message to you
personnaly by mistake.

Alex Sergeyev

unread,
Sep 21, 2012, 11:13:12 PM9/21/12
to golan...@googlegroups.com, kumashi...@gmail.com
Hi GoNuts,

I just run into another caveat in having that error. I just wrote a module that registers itself in registry of another module in its init() function.

Simplified it's

    package A
    import "registry"
    func init() {
        registry.Add(mycode)
    }
    func mycode() {
    }

and then
    package main
    import "A"
    import "registry"
    func main() {
        registry.Action()
    }

Program that has import never calls that module directly, it uses registry object, defined elsewhere. I have workaround but AS IS that code could not be compiled at all IMO it's an irrationality in the compiler.




Alex.

Patrick Mylund Nielsen

unread,
Sep 21, 2012, 11:16:40 PM9/21/12
to Alex Sergeyev, golan...@googlegroups.com, kumashi...@gmail.com
The gc compiler does not have warnings, only errors. This is by design.






Alex.

--
 
 

spete...@gmail.com

unread,
Sep 21, 2012, 11:24:43 PM9/21/12
to Alex Sergeyev, golan...@googlegroups.com, kumashi...@gmail.com
Try this:

import "registry"
import _ "A"

Peter
--
 
 

DisposaBoy

unread,
Sep 22, 2012, 1:03:09 AM9/22/12
to golan...@googlegroups.com, kumashi...@gmail.com


On Saturday, September 22, 2012 4:13:12 AM UTC+1, Alex Sergeyev wrote:
Hi GoNuts,

I just run into another caveat in having that error. I just wrote a module that registers itself in registry of another module in its init() function.
[...]

Please have a thorough read of http://golang.org/ref/spec#Import_declarations
 
Reply all
Reply to author
Forward
0 new messages