Unable to import after declarations
I'm trying to import some modules after the declarations in Go.
For instance, I've tried importing the time time after declaring a variable but that doesn't work , can someone please tell me why that happens in Go?
This works:
package main
import (
"fmt"
)
import "time";
var test = "testing"
func main() {
currtime := time.Now()
fmt.Println(test)
fmt.Println(currtime)//Output: 16:44:53
}
But thid doesn't:
package main
import (
"fmt"
)
var test = "testing"
import "time"
func main() {
currtime := time.Now()
fmt.Println(test)
fmt.Println(currtime)//Output: 16:44:53
}
The error is "non-declaration statement outside function body".
Why does that happen in Go?
go syntax
add a comment |
I'm trying to import some modules after the declarations in Go.
For instance, I've tried importing the time time after declaring a variable but that doesn't work , can someone please tell me why that happens in Go?
This works:
package main
import (
"fmt"
)
import "time";
var test = "testing"
func main() {
currtime := time.Now()
fmt.Println(test)
fmt.Println(currtime)//Output: 16:44:53
}
But thid doesn't:
package main
import (
"fmt"
)
var test = "testing"
import "time"
func main() {
currtime := time.Now()
fmt.Println(test)
fmt.Println(currtime)//Output: 16:44:53
}
The error is "non-declaration statement outside function body".
Why does that happen in Go?
go syntax
I thing this is a choice of the creators so the code is more readable
– Gabriel M
Nov 13 '18 at 10:49
add a comment |
I'm trying to import some modules after the declarations in Go.
For instance, I've tried importing the time time after declaring a variable but that doesn't work , can someone please tell me why that happens in Go?
This works:
package main
import (
"fmt"
)
import "time";
var test = "testing"
func main() {
currtime := time.Now()
fmt.Println(test)
fmt.Println(currtime)//Output: 16:44:53
}
But thid doesn't:
package main
import (
"fmt"
)
var test = "testing"
import "time"
func main() {
currtime := time.Now()
fmt.Println(test)
fmt.Println(currtime)//Output: 16:44:53
}
The error is "non-declaration statement outside function body".
Why does that happen in Go?
go syntax
I'm trying to import some modules after the declarations in Go.
For instance, I've tried importing the time time after declaring a variable but that doesn't work , can someone please tell me why that happens in Go?
This works:
package main
import (
"fmt"
)
import "time";
var test = "testing"
func main() {
currtime := time.Now()
fmt.Println(test)
fmt.Println(currtime)//Output: 16:44:53
}
But thid doesn't:
package main
import (
"fmt"
)
var test = "testing"
import "time"
func main() {
currtime := time.Now()
fmt.Println(test)
fmt.Println(currtime)//Output: 16:44:53
}
The error is "non-declaration statement outside function body".
Why does that happen in Go?
go syntax
go syntax
edited Nov 13 '18 at 11:21
Flimzy
37.7k96497
37.7k96497
asked Nov 13 '18 at 10:41
Uddhav MishraUddhav Mishra
588
588
I thing this is a choice of the creators so the code is more readable
– Gabriel M
Nov 13 '18 at 10:49
add a comment |
I thing this is a choice of the creators so the code is more readable
– Gabriel M
Nov 13 '18 at 10:49
I thing this is a choice of the creators so the code is more readable
– Gabriel M
Nov 13 '18 at 10:49
I thing this is a choice of the creators so the code is more readable
– Gabriel M
Nov 13 '18 at 10:49
add a comment |
3 Answers
3
active
oldest
votes
The error is "non-declaration statement outside function body". Why does that happen in Go?
Because this is how Go is defined to work. From the spec:
Each source file consists of a package clause defining the package to which it belongs, followed by a possibly empty set of import declarations that declare packages whose contents it wishes to use, followed by a possibly empty set of declarations of functions, types, variables, and constants.
This means that the only valid place for import statements is between the package
clause, and declarations of functions, types, variables, and constants.
In your case, you have a variable declaration, so the Go compiler knows there will be no more import statements. Then it sees your erroneous import statement, sees that it is a non-declaration, and correctly generates the error you observe.
add a comment |
why does that happen in go lang ?
Because it is disallowed by the language spec. Pretty simple, or?
add a comment |
You usually have two ways to import packages in Go:
import "fmt"
import "time"
Or
import (
"fmt"
"time"
)
The trick is that you cannot have a mix of import
and something else like in your second example.
package main
import (
"fmt"
)
var test = "testing" //<-- This does not comply with the definition of a Go file
import "time"
func main() {
currtime := time.Now()
fmt.Println(test)
fmt.Println(currtime)//Output: 16:44:53
}
Here you can find a well done documentation of the Anatomy of .go file.
Structure of every .go file is the same.
First is package clause optionally preceded with comments usually describing the purpose of the package.
Then zero or more import declarations.
3rd section contains zero or more top-level declarations
"You usually have two ways.." usually? Except for when?
– Flimzy
Nov 13 '18 at 11:23
Except for the first example given by the OP where he has an ugly mix of both... But even if it's ugly it compiles.
– Nirekin
Nov 13 '18 at 11:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279199%2funable-to-import-after-declarations%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The error is "non-declaration statement outside function body". Why does that happen in Go?
Because this is how Go is defined to work. From the spec:
Each source file consists of a package clause defining the package to which it belongs, followed by a possibly empty set of import declarations that declare packages whose contents it wishes to use, followed by a possibly empty set of declarations of functions, types, variables, and constants.
This means that the only valid place for import statements is between the package
clause, and declarations of functions, types, variables, and constants.
In your case, you have a variable declaration, so the Go compiler knows there will be no more import statements. Then it sees your erroneous import statement, sees that it is a non-declaration, and correctly generates the error you observe.
add a comment |
The error is "non-declaration statement outside function body". Why does that happen in Go?
Because this is how Go is defined to work. From the spec:
Each source file consists of a package clause defining the package to which it belongs, followed by a possibly empty set of import declarations that declare packages whose contents it wishes to use, followed by a possibly empty set of declarations of functions, types, variables, and constants.
This means that the only valid place for import statements is between the package
clause, and declarations of functions, types, variables, and constants.
In your case, you have a variable declaration, so the Go compiler knows there will be no more import statements. Then it sees your erroneous import statement, sees that it is a non-declaration, and correctly generates the error you observe.
add a comment |
The error is "non-declaration statement outside function body". Why does that happen in Go?
Because this is how Go is defined to work. From the spec:
Each source file consists of a package clause defining the package to which it belongs, followed by a possibly empty set of import declarations that declare packages whose contents it wishes to use, followed by a possibly empty set of declarations of functions, types, variables, and constants.
This means that the only valid place for import statements is between the package
clause, and declarations of functions, types, variables, and constants.
In your case, you have a variable declaration, so the Go compiler knows there will be no more import statements. Then it sees your erroneous import statement, sees that it is a non-declaration, and correctly generates the error you observe.
The error is "non-declaration statement outside function body". Why does that happen in Go?
Because this is how Go is defined to work. From the spec:
Each source file consists of a package clause defining the package to which it belongs, followed by a possibly empty set of import declarations that declare packages whose contents it wishes to use, followed by a possibly empty set of declarations of functions, types, variables, and constants.
This means that the only valid place for import statements is between the package
clause, and declarations of functions, types, variables, and constants.
In your case, you have a variable declaration, so the Go compiler knows there will be no more import statements. Then it sees your erroneous import statement, sees that it is a non-declaration, and correctly generates the error you observe.
answered Nov 13 '18 at 11:20
FlimzyFlimzy
37.7k96497
37.7k96497
add a comment |
add a comment |
why does that happen in go lang ?
Because it is disallowed by the language spec. Pretty simple, or?
add a comment |
why does that happen in go lang ?
Because it is disallowed by the language spec. Pretty simple, or?
add a comment |
why does that happen in go lang ?
Because it is disallowed by the language spec. Pretty simple, or?
why does that happen in go lang ?
Because it is disallowed by the language spec. Pretty simple, or?
answered Nov 13 '18 at 11:10
VolkerVolker
20k24758
20k24758
add a comment |
add a comment |
You usually have two ways to import packages in Go:
import "fmt"
import "time"
Or
import (
"fmt"
"time"
)
The trick is that you cannot have a mix of import
and something else like in your second example.
package main
import (
"fmt"
)
var test = "testing" //<-- This does not comply with the definition of a Go file
import "time"
func main() {
currtime := time.Now()
fmt.Println(test)
fmt.Println(currtime)//Output: 16:44:53
}
Here you can find a well done documentation of the Anatomy of .go file.
Structure of every .go file is the same.
First is package clause optionally preceded with comments usually describing the purpose of the package.
Then zero or more import declarations.
3rd section contains zero or more top-level declarations
"You usually have two ways.." usually? Except for when?
– Flimzy
Nov 13 '18 at 11:23
Except for the first example given by the OP where he has an ugly mix of both... But even if it's ugly it compiles.
– Nirekin
Nov 13 '18 at 11:29
add a comment |
You usually have two ways to import packages in Go:
import "fmt"
import "time"
Or
import (
"fmt"
"time"
)
The trick is that you cannot have a mix of import
and something else like in your second example.
package main
import (
"fmt"
)
var test = "testing" //<-- This does not comply with the definition of a Go file
import "time"
func main() {
currtime := time.Now()
fmt.Println(test)
fmt.Println(currtime)//Output: 16:44:53
}
Here you can find a well done documentation of the Anatomy of .go file.
Structure of every .go file is the same.
First is package clause optionally preceded with comments usually describing the purpose of the package.
Then zero or more import declarations.
3rd section contains zero or more top-level declarations
"You usually have two ways.." usually? Except for when?
– Flimzy
Nov 13 '18 at 11:23
Except for the first example given by the OP where he has an ugly mix of both... But even if it's ugly it compiles.
– Nirekin
Nov 13 '18 at 11:29
add a comment |
You usually have two ways to import packages in Go:
import "fmt"
import "time"
Or
import (
"fmt"
"time"
)
The trick is that you cannot have a mix of import
and something else like in your second example.
package main
import (
"fmt"
)
var test = "testing" //<-- This does not comply with the definition of a Go file
import "time"
func main() {
currtime := time.Now()
fmt.Println(test)
fmt.Println(currtime)//Output: 16:44:53
}
Here you can find a well done documentation of the Anatomy of .go file.
Structure of every .go file is the same.
First is package clause optionally preceded with comments usually describing the purpose of the package.
Then zero or more import declarations.
3rd section contains zero or more top-level declarations
You usually have two ways to import packages in Go:
import "fmt"
import "time"
Or
import (
"fmt"
"time"
)
The trick is that you cannot have a mix of import
and something else like in your second example.
package main
import (
"fmt"
)
var test = "testing" //<-- This does not comply with the definition of a Go file
import "time"
func main() {
currtime := time.Now()
fmt.Println(test)
fmt.Println(currtime)//Output: 16:44:53
}
Here you can find a well done documentation of the Anatomy of .go file.
Structure of every .go file is the same.
First is package clause optionally preceded with comments usually describing the purpose of the package.
Then zero or more import declarations.
3rd section contains zero or more top-level declarations
edited Nov 13 '18 at 11:07
answered Nov 13 '18 at 10:59
NirekinNirekin
3,31111641
3,31111641
"You usually have two ways.." usually? Except for when?
– Flimzy
Nov 13 '18 at 11:23
Except for the first example given by the OP where he has an ugly mix of both... But even if it's ugly it compiles.
– Nirekin
Nov 13 '18 at 11:29
add a comment |
"You usually have two ways.." usually? Except for when?
– Flimzy
Nov 13 '18 at 11:23
Except for the first example given by the OP where he has an ugly mix of both... But even if it's ugly it compiles.
– Nirekin
Nov 13 '18 at 11:29
"You usually have two ways.." usually? Except for when?
– Flimzy
Nov 13 '18 at 11:23
"You usually have two ways.." usually? Except for when?
– Flimzy
Nov 13 '18 at 11:23
Except for the first example given by the OP where he has an ugly mix of both... But even if it's ugly it compiles.
– Nirekin
Nov 13 '18 at 11:29
Except for the first example given by the OP where he has an ugly mix of both... But even if it's ugly it compiles.
– Nirekin
Nov 13 '18 at 11:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279199%2funable-to-import-after-declarations%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I thing this is a choice of the creators so the code is more readable
– Gabriel M
Nov 13 '18 at 10:49