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).
Related
I have a layer where the path of node_modules is nodejs/node14/node_modules.
Using that layer, and I try to import a package in a Lambda function, say 'aws-cloudfront-sign', like this:
import cfsign from 'aws-cloudfront-sign'
I got error message
Cannot find package 'aws-cloudfront-sign' imported from /var/task/signer.js\nDid you mean to import aws-cloudfront-sign/lib/cloudfrontUtil.js?
But if I import the package like this:
import cfsign from '/opt/nodejs/node14/node_modules/aws-cloudfront-sign/lib/cloudfrontUtil.js'
It succeeds.
Do you know why? How could I import the package correctly?
This appears to be a bug. It is occurring with layers and the SDK. There are are a number of similar open issues on Github:
Nodejs Lambda: Cannot find package 'aws-sdk'
Cannot find package when using ES Module and Lambda Layer
ES6 imports don't work in #aws-sdk/client-iotsitewise
As you have worked out, the only workaround at present seems to be the use of absolute paths. E.g.:
import { DynamoDB } from 'aws-sdk;'
fails, whereas
import AWS from '/var/runtime/node_modules/aws-sdk/lib/aws.js';
const { DynamoDB } = AWS;
will work.
I suggest you add your voice to an existing open issue to help ensure it gets attention.
I'm trying to create a package that exports functionality using useQuery
However I get the following error:
Could not find "client" in the context or passed in as an option. Wrap the root component in an <ApolloProvider>, or pass an ApolloClient instance in via options.
This is even with simplest possible example which just exports useQuery (obvs real code does more than this)
I know my code is ok otherwise as if I import useQuery using
import { useQuery } from '#apollo/react-hooks'
it works fine
simple example is here..
https://github.com/gilesbradshaw/use-query
The hook and the ApolloProvider used should be from the same module, otherwise the context used by the hook will be different than what is provided by the ApolloProvider. You should export ApolloProvider in your package in addition to the hook, and then make sure you import it from your package whereever you're using the hook.
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.
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".