Hello members of the Golang Forum,
I’m making a webserver in go this is my first time coding in go btw but i get this error when i try to run my webserver that i made import cycle not allowed. I don’t know why this happens so help will be appreciated
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.Run()
}
gitpod /workspace/coding (main) $ go run webserver.go
package command-line-arguments
imports GitHub - gin-gonic/gin: Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
imports github.com/gin-gonic/gin: import cycle not allowed
gitpod /workspace/coding (main) $
Sibert
(Sibert)
March 18, 2024, 4:29pm
2
Try first without framework and take it from there:
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprint(w, "Hello World")
}
func main() {
http.HandleFunc("/", handler)
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println("Error:", err)
}
}
And may I ask why you need framework? IMHO the built in libraries is good enough…
Live hello!
pekim
(Mike D Pilsbury)
March 18, 2024, 5:19pm
4
Did you create a go.mod
, with something along the lines of go mod init my-base-package-name
?
The following works just fine for me.
$ mkdir test-server
$ cd test-server/
$ go mod init test-server
go: creating new go.mod: module test-server
$ go get github.com/gin-gonic/gin
go: added github.com/bytedance/sonic v1.9.1
go: added github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311
go: added github.com/gabriel-vasile/mimetype v1.4.2
go: added github.com/gin-contrib/sse v0.1.0
go: added github.com/gin-gonic/gin v1.9.1
go: added github.com/go-playground/locales v0.14.1
go: added github.com/go-playground/universal-translator v0.18.1
go: added github.com/go-playground/validator/v10 v10.14.0
go: added github.com/goccy/go-json v0.10.2
go: added github.com/json-iterator/go v1.1.12
go: added github.com/klauspost/cpuid/v2 v2.2.4
go: added github.com/leodido/go-urn v1.2.4
go: added github.com/mattn/go-isatty v0.0.19
go: added github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: added github.com/modern-go/reflect2 v1.0.2
go: added github.com/pelletier/go-toml/v2 v2.0.8
go: added github.com/twitchyliquid64/golang-asm v0.15.1
go: added github.com/ugorji/go/codec v1.2.11
go: added golang.org/x/arch v0.3.0
go: added golang.org/x/crypto v0.9.0
go: added golang.org/x/net v0.10.0
go: added golang.org/x/sys v0.8.0
go: added golang.org/x/text v0.9.0
go: added google.golang.org/protobuf v1.30.0
go: added gopkg.in/yaml.v3 v3.0.1
$ cat << EOF > test-server.go
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.Run()
}
EOF
$ go run test-server.go
packs
(Prakash)
March 19, 2024, 4:02am
5
You have not mentioned the listen port
pekim
(Mike D Pilsbury)
March 19, 2024, 6:14am
6
gin defaults to listening on port 8080.
packs
(Prakash)
March 19, 2024, 6:32am
7
Yes. For safer side you assign other port and check…
May be 8080 port is using other applications
pekim
(Mike D Pilsbury)
March 19, 2024, 6:45am
8
The problem that the original poster had was with compilation. They’d not got as far as running the built application.
packs
(Prakash)
March 19, 2024, 7:35am
9
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.Run()
}
I have tested this code it is working
net/http add in your code
http://localhost:8080/ping
{
“message”: “pong”
}
in postman