I can't install a go package - go

i'm currently try to install https://github.com/willnorris/imageproxy . I'm a newbie with go lang. So i installed first go, github and hg.
I tried a few things, clone the git repository, use go get, use go install, go build.
But nothing worked, it could download the files to the src direcotry, but didn't get the bin.
so has anybody an idea?
---edit---
with:
go get github.com/willnorris/imageproxy/cmd/imageproxy
i get:
[root#s17848415 go]# go get github.com/willnorris/imageproxy/cmd/imageproxy
# github.com/willnorris/imageproxy/cmd/imageproxy
src/github.com/willnorris/imageproxy/cmd/imageproxy/main.go:27: imported and not used: "github.com/sosedoff/imageproxy/proxy"
src/github.com/willnorris/imageproxy/cmd/imageproxy/main.go:60: undefined: imageproxy
--edit 2 ---
[root#s17848415 go]# go get github.com/willnorris/imageproxy/cmd/imageproxy
package willnorris.com/go/imageproxy: unrecognized import path "willnorris.com/go/imageproxy"

solution:
first:
go get github.com/willnorris/imageproxy/cmd/imageproxy
(downloads source)
second - change "vi github.com/willnorris/imageproxy/cmd/imageproxy"
last line
import (
"flag"
"fmt"
"log"
"net/http"
"strings"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
"github.com/willnorris/imageproxy"
)
last - again:
go get github.com/willnorris/imageproxy/cmd/imageproxy

I've updated the README file to include getting started instructions: https://github.com/willnorris/imageproxy#getting-started. Notably, you should go get as "willnorris.com/go/imageproxy", not "github.com/willnorris/imageproxy".

Related

Cannot import package "google.golang.org/api/compute/v1"

I am using cloud function to auto create some VM instance. Before, I was success in my use-case. You can view it here.Now, I want to update some line in metadata script. But when I save the code, it tell me some package I was import cannot find. Logs is
Build failed: src/cloudfunctions/function.go:9:2: cannot find package "google.golang.org/api/compute/v1" in any of:
/usr/local/go/src/google.golang.org/api/compute/v1 (from $GOROOT)
/workspace/src/google.golang.org/api/compute/v1 (from $GOPATH); Error ID: 2f5e35a0
But I was view document in https://pkg.go.dev/google.golang.org/api/compute/v1, usage example import like this import "google.golang.org/api/compute/v1", as same as me.
So, who can tell me what is $GOROOT and $GOPATH in cloud function, and how can I import this package.
Thanks in advance.
Run
go get -u -x google.golang.org/api/compute/v1
Then cd/open your directory in terminal then run
go mod init filename.go
then
go mod tidy
Also restart your IDE as that will need to update

JetBrains $GOPATH/go.mod exists but should not

Very simple code is not working. Can't find what's problem.
In console no one request didn't work. Because I dont know how use go-terminal in console. Maybe I can change some settings?
go.mod contains module "awesomeProject"
package main
import "fmt"
func main() {
fmt.Println("some text")
}
GOROOT=C:\Users\olli\sdk\go1.16.3 #gosetup
GOPATH=D:\Projects\GoLang #gosetup
C:\Users\olli\sdk\go1.16.3\bin\go.exe build -o C:\Users\olli\AppData\Local\Temp___go_build_awesomeProject.exe awesomeProject #gosetup
$GOPATH/go.mod exists but should not
Compilation finished with exit code 1
UPD:
I didn't fix the problem, just erased it and installed everything in the base folders, without changing it, and everything worked.
try to initialize the module with;
go mod init awesomeProject

Import local package that's already in public repo

I am working on building a web framework in Go. I have the code locally and would like to use that repo in another module to test that everything works without having to create tags and/or push things to remote repos. I have followed the official docs on how to do this, as well as a number of posts elsewhere. However nothing seems to work. What am I doing incorrectly?
Package live here locally:
../goworkspace/src/github.com/garrettlove8/goserve
Import from other module:
...
import (
"fmt"
"io"
"net/http"
"github.com/garrettlove8/goserve" // I have tried "goserve" and "../goserve"
)
...
go.mod:
...
require github.com/garrettlove8/goserve v0.1.17
No matter what I do it doesn't seem to work as desired
Update
Code and error combos:
// main.go
import (
"fmt"
"io"
"net/http"
"goserve"
)
Run go mod tidy
// go.mod becomes
require github.com/garrettlove8/goserve v0.1.17 // indirect
Error:
goserve: package goserve is not in GOROOT (/usr/local/go/src/goserve)
Manually changing go mod to this (which I don't to have to do):
require github.com/garrettlove8/goserve
Run go mod tidy
Error:
usage: require module/path v1.2.3
When you want to use a local module instead of a remote one, you can achieve that with the replace directive.
In your case, add this to your go.mod file:
replace github.com/garrettlove8/goserve => ../goworkspace/src/github.com/garrettlove8/goserve

Can't import packages

I'm trying to import a subdirectory I have in my project to my main file, but for some reason I get this error:
could not import ./service (no package for import ./service)
This is how I import:
import (
"os"
"service"
)
I also tried "service/app"
"./service/app"
"./service" nothing works, always the same error.
I'm trying to use the app.go file
This is the file structure:
Project Directory
-- main.go
-- service (directory)
-- app.go (file)
I tried to restart VS Code, tried to use go install/update tools, nothing works.
Also, this is my main func:
func main() {
a := &service.App()
a.Initialize(
os.Getenv("APP_DB_USERNAME"),
os.Getenv("APP_DB_PASSWORD"),
os.Getenv("APP_DB_NAME"),
)
a.Run(":8010")
}
&service.App() does not show a problem, but when I remove the "service" import, it does say
undeclared name: service
So I don't understand what the problem is.
This error sometime show on the "os" import too, I don't know why.
Golang doesn't support relative imports.
In modules, there finally is a name for the subdirectory. If the parent directory says "module m" then the subdirectory is imported as "m/subdir", no longer "./subdir".
So, in your case you should import service package as your_module_name/service.

How do I set GODEBUG environment variables in Golang so I can use godebug with net/http

I want to step through my program using Godebug. However because I'm using net/http I get errors such as:
/home/heath/go/src/net/http/h2_bundle.go:45:2: could not import golang_org/x/net/http2/hpack (cannot find package "golang_org/x/net/http2/hpack" in any of:
/home/heath/go/src/golang_org/x/net/http2/hpack (from $GOROOT)
/x/net/http2/hpack does exist in my GOPATH but in ~heath/go/src/golang.org ... not golang_org (not sure what's happening there)
I have read that this error occurs because godebug doesn't support http2 yet (can't find source).
I have tried to disable http2server and http2client by setting the GODEBUG env both in init() and on the command line. I have also confirmed that these settings are being set by doing a fmt.Println("GODEBUG", os.Getenv("GODEBUG"). As per instructions located here
GODEBUG=http2client=0 # disable HTTP/2 client support
GODEBUG=http2server=0 # disable HTTP/2 server support
My simple code example to replicate the error is:
package main
import "fmt"
import "net/http"
import "os"
func init() {
os.Setenv("GODEBUG", "http2server=0,http2client=0")
}
func main() {
fmt.Println("GODEBUG", os.Getenv("GODEBUG"))
_ = "breakpoint"
fmt.Println("Hello, World!")
http.ListenAndServe(":8080", nil)
}
godebug run example.go
I am running Go version:
go version go1.7 linux/amd64
This is probably not the correct way to do things. But I copied the http2 package from my vendor directory under $GOROOT/src/vendor to the src directory under my $GOPATH/src.
If anyone has any further input on the correct way to reference vendor directories please add your thoughts. Just putting my workaround here in case someone else comes across the same issue.
Edit: Actually a 'nicer' way to do things was to do an ln -s ..src/vender/github_org to ../src/github_org

Resources