Conflicting type in golang compilation [duplicate] - go

I'm trying to use this Golang Yelp API package. In some of its structs, it uses types defined in guregu's null package.
I want to declare a struct defined in the Yelp API package, where some of its fields have null.Float as a value (i.e. this struct, which im trying to use). So in my program, I import both the Yelp API package and guregu's null package and try to declare the struct, with ip.Lat and ip.Lat being float64s. (null.FloatFrom definition):
33 locationOptions := yelp.LocationOptions{
34 ip.Zip,
35 &yelp.CoordinateOptions{
36 Latitude: null.FloatFrom(ip.Lat),
37 Longitude: null.FloatFrom(ip.Lon),
38 },
39 }
But when I run the program, it tells me:
./cli.go:36: cannot use "github.com/guregu/null".FloatFrom(ip.Lat) (type
"github.com/guregu/null".Float) as type "github.com/JustinBeckwith/go-
yelp/yelp/vendor/github.com/guregu/null".Float in field value
I tried 2 things:
1) I did not import the null package, which caused Go to complain about null being undefined. 2) I also tried importing the vendored package directly, which caused Go to tell me use of vendored package not allowed.
Any Ideas on how to fix this?

The solution here seems to be that the library I'm trying to use needs to be reworked to prevent this kind of thing from happening.
The two possible ways to change the library seem to be
1) not vendor at all - this works if the dependency does not need to be a specific version.
2) vendored, but do not expose the vendored library to the public. Create some wrapper functions in the library so that people can create the types indirectly.
See this discussion about vendoring on reddit for more ideas/reasons why.

I had the same issue. As a work around, I deleted the associated package's vendor folder and moved their content to my $GOPATH folder.
Source of answer: promql Go import problem: use of vendored package not allowed

Just had a similar issue. Putting both libraries in /vendor resolved. Using govendor get xxxx

Had a similar issue while using Godep and I resolved by deleting /vendor and re-running godep save ./... - Hope it helps.

Related

PromQL Module Missing

I am trying to use the promql package here
package main
import (
"fmt"
"github.com/prometheus/prometheus/promql/parser"
)
func main() {
fmt.Println("Hello")
parser.ParseExpr("foobar")
}
Having trouble importing. This is the error:
no required module provides package
github.com/prometheus/prometheus/promql/parser; to add it:
go get github.com/prometheus/prometheus/promql/parser (compile)
I tried to run go get github.com/prometheus/prometheus/promql/parser as suggested but it fails.
go get: module github.com/prometheus/prometheus#upgrade found
(v2.5.0+incompatible), but does not contain package
github.com/prometheus/prometheus/promql/parser
Here is my go.mod currently:
module foo.com/bar/parser
go 1.17
require github.com/prometheus/prometheus v2.5.0+incompatible // indirect
Use go get github.com/prometheus/prometheus#83032011a5d3e6102624fe58241a374a7201fee8 (that commit is the latest release at this point in time, v2.33.4)
The reason this is needed is that
This is a known issue with Go Modules. The semantic versioning of Prometheus versions the behavior of Prometheus as a server, not its code as a library. By changing the module path to v2, we would suggest that Prometheus obeys the contract of Go Modules as a library, but it doesn't, i.e. there are many breaking changes to expect even in a minor release.
and:
Prometheus was not intended to be used as a library. Now that has changed, and it is intended to be used as such, even if we do not accept all general-purpose contributions.
The error you are seeing is because go get is grabbing an old release v2.5.0 by default which was released back in 2018 and does not include the parser package. This happens because the versioning scheme used by Prometheus does not align with that assumed by Go.
See this issue for additional info.

Get ShaXXX of a GO Package

I Need to get the Sha512 or similar from a Golang package for SBOM purposes.
For example, the hash for package
https://pkg.go.dev/encoding/json
I can't found any information or api to get it. If possible I need it without download the source code.
I think you are looking for sum.golang.org which is
an auditable checksum database which will be used by the go command to
authenticate modules.
you can read more on how it works on this post from go blog

Using local packages with gb

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.

package's type cannot be used as the vendored package's type

I'm trying to use this Golang Yelp API package. In some of its structs, it uses types defined in guregu's null package.
I want to declare a struct defined in the Yelp API package, where some of its fields have null.Float as a value (i.e. this struct, which im trying to use). So in my program, I import both the Yelp API package and guregu's null package and try to declare the struct, with ip.Lat and ip.Lat being float64s. (null.FloatFrom definition):
33 locationOptions := yelp.LocationOptions{
34 ip.Zip,
35 &yelp.CoordinateOptions{
36 Latitude: null.FloatFrom(ip.Lat),
37 Longitude: null.FloatFrom(ip.Lon),
38 },
39 }
But when I run the program, it tells me:
./cli.go:36: cannot use "github.com/guregu/null".FloatFrom(ip.Lat) (type
"github.com/guregu/null".Float) as type "github.com/JustinBeckwith/go-
yelp/yelp/vendor/github.com/guregu/null".Float in field value
I tried 2 things:
1) I did not import the null package, which caused Go to complain about null being undefined. 2) I also tried importing the vendored package directly, which caused Go to tell me use of vendored package not allowed.
Any Ideas on how to fix this?
The solution here seems to be that the library I'm trying to use needs to be reworked to prevent this kind of thing from happening.
The two possible ways to change the library seem to be
1) not vendor at all - this works if the dependency does not need to be a specific version.
2) vendored, but do not expose the vendored library to the public. Create some wrapper functions in the library so that people can create the types indirectly.
See this discussion about vendoring on reddit for more ideas/reasons why.
I had the same issue. As a work around, I deleted the associated package's vendor folder and moved their content to my $GOPATH folder.
Source of answer: promql Go import problem: use of vendored package not allowed
Just had a similar issue. Putting both libraries in /vendor resolved. Using govendor get xxxx
Had a similar issue while using Godep and I resolved by deleting /vendor and re-running godep save ./... - Hope it helps.

get apex package prefix name?

is there a way to get the prefix name of a managed package in apex?
I have a SOSL query but the app is in a developer org and in a managed package, if I have a way to get the package name, it would be great, because I don't have to hard code it.
Thanks.
The only way to do this, AFAIK, is to use the Metadata API. You can call describeMetadata() and then evaluate the organizationNamespace (String) value returned in describeMetadataResult.
From within Apex you could do a less elegant try/catch approach with dynamic SOQL/DML, at least to determine if something you suspect is there is actually there. Though this won't tell you what namespace prefix actually IS there like the Metadata API will.
Lacey is correct that you don't need the prefix, though name ambiguity can become a problem. So for example, if you have a custom object Expense__c and have installed an accounting package which includes ACCT__Expense__c, you definitely want to explicitly include the ACCT__ prefix if intending to access the managed package object as opposed to your own.
The UserInfo.isCurrentUserLicensed('nsPrefix') will throw a TypeException if the namespace passed in is not a valid namespace of an installed package (or managed package you're developing). I think this is the closest you're going to get without the metadata API.

Resources