here the simple go application. I am getting "go run: cannot run non-main package" error, if I run following code.
package zsdfsdf
import (
"fmt"
)
func Main() {
fmt.Println("sddddddd")
}
To fix it, I just need to name the package to main. But I don't understand why I need to do that. I should be able to name the package whatever I want.
Another question, I know main function is the entry point of the program, you need it. otherwise it will not work. But I see some codes that didn't have main function still works.
Click on this link, the example at the bottom of the page didn't use package main and main function, and it still works. just curious why.
https://developers.google.com/appengine/docs/go/gettingstarted/usingdatastore
The entry point of each go program is main.main, i.e. a function called main in a package called main. You have to provide such a main package.
GAE is an exception though. They add a main package, containing the main function automatically to your project. Therefore, you are not allowed to write your own.
You need to use the main package, a common error starting with go is to type
package Main
instead of
package main
You need to specify in your app.yaml file what your app access point is. Take a look here. You need to specify:
application: zsdfsdf
Also see from that above link:
"Note: When writing a stand-alone Go program we would place this code
in package main. The Go App Engine Runtime provides a special main
package, so you should put HTTP handler code in a package of your
choice (in this case, hello)."
You are correct that all Go programs need the Main method. But it is provided by Google App Engine. That is why your provided example works. Your example would not work locally (not on GAE).
A Solution to avoid this error is defining entry point somefilename.go file as main package by adding package main as the first line of the entry point
package main
// import statements
import "fmt"
// code below
To avoid the problem you can modify the code as follow
package main
import (
"fmt"
)
func main() {
fmt.Println("sddddddd")
}
rename the package as "main" and rename the function as "main" instead of "Main".
Related
Here is a quote from a blog post:
Enforce vanity URLs go get supports getting packages by a URL that is
different than the URL of the package's repo. These URLs are called
vanity URLs and require you to serve a page with specific meta tags
the Go tools recognize. You can serve a package with a custom domain
and path using vanity URLs.
For example,
$ go get cloud.google.com/go/datastore
checks out the source code from
https://code.googlesource.com/gocloud behind the scenes and puts it in
your workspace under $GOPATH/src/cloud.google.com/go/datastore.
Given code.googlesource.com/gocloud is already serving this package,
would it be possible to go get the package from that URL? The answer
is no, if you enforce the vanity URL.
To do that, add an import statement to the package. The go tool will
reject any import of this package from any other path and will display
a friendly error to the user. If you don't enforce your vanity URLs,
there will be two copies of your package that cannot work together due
to the different namespace.
package datastore // import "cloud.google.com/go/datastore"
Can someone explain what the last line means?
If you don't enforce your vanity URLs,
there will be two copies of your package that cannot work together due
to the different namespace.
Or demonstrate it in an example?
It's pretty simple.
If your package is hosted at, say github.com/foo/bar, and you have vanity URL foo.com/bar, then someone could do both:
import "github.com/foo/bar"
and
import "foo.com/bar"
This will be problematic if you have different files or packages that import the same package at different paths.
Imagine these two files in the same package:
// foo.go
package foo
import "github.com/foo/bar"
func frobnicate(x *bar.Something) { /* ... */ }
// bar.go
package foo
import "foo.com/bar"
func widget() {
x := *bar.Something{}
frobnicate(x) // compilation error: cannot use x (type foo.com/bar.Something) as type github.com/foo/bar.Something
}
When developing a small Google Cloud Function in Go. I noticed it will throw an error if you have everything in your package main - eg. import "<whatever>" is a program, not an importable package
So the solution is switch it out to its own package, then deploy. If something goes wrong, throw it back into a package main and work on it locally, then switch it back.
Is this the best workflow? The other option i see is possibly making the Cloud Function its own module and importing it into a main.go file.
I was able to create a cli folder in project's top level and then put main.go file using package main and main() function inside it. That allowed me to have separate file cloud_functions.go in root with different package name that has one or more google cloud functions in it.
I'm starting a new project and considering gb as my build tool but it doesn't appear to be integrating very well with vscode...
I've referenced 3rd party dependencies no problem using gb vendor fetch but as for creating local packages, this is proving a little trickier! Am I missing something obvious?
Here's my local src directory:
src
/cmd
/model
calc.go
/server
server.go
The following code compiles and creates a bin\server.exe file successfully but the import path isn't picked up, nor does gocode recognise it
Here's the server code:
package main
import (
"cmd/model" // not a happy reference...
"fmt"
)
func main() {
fmt.Println(model.Add(1, 2))
}
Here's the model code:
package model
func Add(a int, b int) int {
return a + b
}
I've found what appears to be a similar issue on Github (https://github.com/joefitzgerald/go-plus/issues/325) and while nsf's solution sorts out auto-complete (post import), the import statement itself still claims to be searching in the GOROOT and GOPATHs.
Any ideas?
Thanks to an answer from lukehoban here https://github.com/Microsoft/vscode-go/issues/249 I was able to get my environment working.
I simply created a settings.json file under the .vscode directory (which will now have to be checked in) into which I've configured:
{
"go.gopath": "${workspaceRoot}"
}
This makes me feel unclean and it still doesn't provide a way to reference both 3rd party dependencies and local packages together...
Do not try to work against Go, work with Go.
First of all give all your packages fully qualified import paths. Go is designed around global import paths, do not try to force Go into using flat hierarchies or even relative paths.
You can point to your import path repository endpoints either directly or by using Go's remote import path mechanism. BTW, if you happen to run a self-hosted GitLab instance, it supports remote import path meta tags out of the box.
I prefer glide, but maybe the following is possible with gb, too. Certainly something simililar will be possible with the upcoming go dep: You can point to ssh+git endpoints and others using glide's repo stanza. Frankly I have no idea if gb supports an equivalent mechanism, but if it doesn't this is a good reason to reconsider.
I haven't used Go in a while and I'm just starting to work on an old project again.
I have init() functions in a number of the packages and they work fine. However I've just created a new package and added an init() function but it won't run during initialization like the others. If I put the init() function in an previously existing package it runs fine...
I believe this is a simple problem but I can't for the life of me figure it out. What could I be doing wrong?
If you main program does not import your new package at all... its init() function would not be called.
If you just want an imported package's init() function to be executed, and don't want to use package's other content, you should modify import "foo" to import _ "foo".
See init function (and its full documentation in program execution).
Is there a way to import external modules into one file and then import from that file?
For example:
// externals.go
import (
Bitbucket "bitbucket.org/user/project"
Github "github.com/user/project"
)
// main.go
import (
"externals/Bitbucket"
"externals/Github"
)
Is the above in some form possible?
No, this is not possible. It is a specific design goal of Go
to make all dependencies explicit.
See http://talks.golang.org/2012/splash.article and section 7
in particular for more detail on this.
No. This is not possible, not even with some tricks.