Hi Team,
I want to try httprouter, but unable to install it, I am getting the below error.
What is the solution for the same
I tried all possible commands like:
go install <path to HTTP router@latest" but nothign works!
Hi Team,
I want to try httprouter, but unable to install it, I am getting the below error.
What is the solution for the same
I tried all possible commands like:
go install <path to HTTP router@latest" but nothign works!
Hi @shubhra,
It seems you do not have a go.mod
file yet.
Run go mod init mymodule
, then run go get
to download httprouter
. Replace mymodule
with the name that you want to give your module.
(Pro tip: It is common practice to use a path-like string here, like go mod init github.com/myorg/mymodule
. This way, the module can be published to a remote repository later without changing the module path.)
@christophberger I believe it worked. But, still not sure though, as I am using windows and vscode and I am not sure where is the cat path. But, why I keep getting this windows firewall blocker, whenever I run go run main.go command
Two queries : Mod file we generate inside a module or outside a module.
package main
import (
"bytes"
"fmt"
"log"
"net/http"
"os/exec"
"github.com/julienschmidt/httprouter"
)
// This is a function to execute a system command and return output
func getCommandOutput(command string, arguments ...string) string {
// args... unpacks arguments array into elements
cmd := exec.Command(command, arguments...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Start()
if err != nil {
log.Fatal(fmt.Sprint(err) + ": " + stderr.String())
}
err = cmd.Wait()
if err != nil {
log.Fatal(fmt.Sprint(err) + ": " + stderr.String())
}
return out.String()
}
func goVersion(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
//fmt.Fprintln("I am Mike John and go version is")
fmt.Fprintf(w, getCommandOutput("C:/Program Files/Go/bin/go", "version"))
}
func getFileContent(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
fmt.Fprintf(w, getCommandOutput("/bin/cat", params.ByName("name")))
}
func main() {
router := httprouter.New()
// Mapping to methods is possible with HttpRouter
router.GET("/api/v1/go-version", goVersion)
// Path variable called name used here
router.GET("/api/v1/show-file/:name", getFileContent)
log.Fatal(http.ListenAndServe(":8000", router))
}
@christophberger I tried creating “go mod init htttprouter” inside the folder httprouter
This is normal. You start a web server that can receive connection requests from outside your machine. The operating system considers this a potential security hole (because the OS does not know whether your web server is evil or not) and warns you.
Try changing the ListenAndServe call to
http.ListenAndServe("localhost:8000", router)
Then the server listens only to requests from localhost, and Windows should not complain anymore.
The go.mod
file lives in the root directory of a module. See also: Managing module source - The Go Programming Language.
I am not quite sure what you mean. What line is highlighted? If you hover the mouse cursor over the highlighted line, does the editor display an explanation for the highlight?
The file structure looks right to me. go.mod
and go.sum
are side-by-side with main.go
.
@christophberger I tried getting the GIT version using the command and it worked. And there are two yellow lines in the code, unable to understand why it is so?
But, what is the linux command ( cat) equivalent in windows?
Our replies crossed their way.
Not sure, try hovering the mouse cursor over the lines to get more info. Or click on the PROBLEMS tab in the lower pane.
I haven’t used Windows since 2008, so I am afraid I cannot help with Windows-specific questions. A quick web search indicates that the equivalent to cat
is type
.
However, based on this discussion, there seem to be some issues with type
(see the comments below the accepted answer), so some people seem to prefer copy /b
(see the second answer).
@christophberger Wow! using localhost the problem of windows firewall is resolved. You are awesome. When I try to hover over yellow lines, you can see in the below screenshot, what is the error/problem it shows. Please let me know if there is a solution for the same.
Also, what is the cat equivalent command in windows to cat the entire file content?
So, important learning are below
Important notes:
1. Use localhost to avoid getting windows firewall screen : log.Fatal(http.ListenAndServe(“localhost:8000”, router))
2. For install a router, first use the below command
git mod init httprouter
git tidy
git install GitHub - julienschmidt/httprouter: A high performance HTTP request router that scales well
@christophberger Sorry! the last screenshot doesn’t shows you warnings for yellow lines.
@christophberger Yes that is copy command but not able to find it’s path in windows.
what should be the path for it?
This is a warning from a linter (go-staticcheck
). It basically says that you do not need to use Fprintf()
if you do not use a format string. (That is, a string with “verbs” like %s
or %d
that map to subsequent arguments. Example: fmt.Fprintf(w, "x is %d and y is %s", x, y)
.)
Simply use Fprint()
or Fprintln()
to get rid of the warning.
@christophberger You are simply Awesome. Thank you for giving all the pointers and they worked. Lots of love and care.
Do you how how to make that copy \b work in vscode (windows) ? What exact line to write?
I am not able to find the path of copy though. I have posted their link in the above chats.
If you can find no path to copy
, then maybe copy
is a built-in command? I am not sure how this works in Windows. Unix shells do have built-in commands like pushd
or popd
.
Maybe try invoking copy
without a path.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.