Error undefined: imagick.Initialize while using imagick - go

I am using the gopkg.in/gographics/imagick.v2/imagick package but it shows the error while running the server : undefined: imagick.Initialize
import "gopkg.in/gographics/imagick.v2/imagick"
func ConvertPdfToJpg() {
imagick.Initialize()
defer imagick.Terminate()
mw := imagick.NewMagickWand()
defer mw.Destroy()
mw.ReadImage("test.pdf")
mw.SetIteratorIndex(0)
mw.SetImageFormat("jpg")
mw.WriteImage("test.jpg")
}

Please check out readme from the project.
Go Imagick is a Go bind to ImageMagick's MagickWand C API.
You need to install ImagMagick first, and then you can use go bindings.

Related

Broken dependencies to encoding pkg. Code not working from one day to the other. Failed upgrade? (EDIT: No! failed user. Please see my answer)

I am new to Golang and am developing a web scraping program. Last night I was running my programs and all was OK. My programs ran both from source code using "go run xyz.go", or a built version ($ ./xyz.exe)
This morning I woke up to an apparent broken dependency problem that I have not managed to figure out. The built program (./xyz.exe) from last night still runs, but when I try to run the source code today using "go run xyz.go" I get the following result:
"$ go run xyz.go
go build golang.org/x/text/encoding: no non-test Go files in C:\Users\admin\Dropbox\gowork\pkg\mod\golang.org\x\text#v0.3.7\encoding
go build golang.org/x/text/transform: no non-test Go files in C:\Users\admin\Dropbox\gowork\pkg\mod\golang.org\x\text#v0.3.7\transform
golang.org/x/net/html/atom
........\pkg\mod\golang.org\x\net#v0.0.0-20210916014120-12bc252f5db8\html\atom\atom.go:27:26: undefined: atomText
........\pkg\mod\golang.org\x\net#v0.0.0-20210916014120-12bc252f5db8\html\atom\atom.go:30:9: undefined: atomText
........\pkg\mod\golang.org\x\net#v0.0.0-20210916014120-12bc252f5db8\html\atom\atom.go:34:9: undefined: atomText
........\pkg\mod\golang.org\x\net#v0.0.0-20210916014120-12bc252f5db8\html\atom\atom.go:58:29: undefined: maxAtomLen
........\pkg\mod\golang.org\x\net#v0.0.0-20210916014120-12bc252f5db8\html\atom\atom.go:61:11: undefined: hash0
........\pkg\mod\golang.org\x\net#v0.0.0-20210916014120-12bc252f5db8\html\atom\atom.go:62:10: undefined: table
........\pkg\mod\golang.org\x\net#v0.0.0-20210916014120-12bc252f5db8\html\atom\atom.go:65:10: undefined: table
github.com/andybalholm/cascadia
........\pkg\mod\github.com\andybalholm\cascadia#v1.2.0\serialize.go:10:9: undefined: tagSelector
........\pkg\mod\github.com\andybalholm\cascadia#v1.2.0\serialize.go:14:9: undefined: idSelector
........\pkg\mod\github.com\andybalholm\cascadia#v1.2.0\serialize.go:18:9: undefined: classSelector
........\pkg\mod\github.com\andybalholm\cascadia#v1.2.0\serialize.go:22:9: undefined: attrSelector
........\pkg\mod\github.com\andybalholm\cascadia#v1.2.0\serialize.go:32:9: undefined: relativePseudoClassSelector
........\pkg\mod\github.com\andybalholm\cascadia#v1.2.0\serialize.go:35:9: undefined: containsPseudoClassSelector
........\pkg\mod\github.com\andybalholm\cascadia#v1.2.0\serialize.go:42:9: undefined: regexpPseudoClassSelector
........\pkg\mod\github.com\andybalholm\cascadia#v1.2.0\serialize.go:49:9: undefined: nthPseudoClassSelector
........\pkg\mod\github.com\andybalholm\cascadia#v1.2.0\serialize.go:75:9: undefined: onlyChildPseudoClassSelector
........\pkg\mod\github.com\andybalholm\cascadia#v1.2.0\serialize.go:81:9: undefined: inputPseudoClassSelector
........\pkg\mod\github.com\andybalholm\cascadia#v1.2.0\serialize.go:81:9: too many errors
golang.org/x/text/internal/language
........\pkg\mod\golang.org\x\text#v0.3.7\internal\language\compact.go:12:23: undefined: Tag
........\pkg\mod\golang.org\x\text#v0.3.7\internal\language\compact.go:23:32: undefined: Tag
........\pkg\mod\golang.org\x\text#v0.3.7\internal\language\compose.go:15:6: undefined: Tag
........\pkg\mod\golang.org\x\text#v0.3.7\internal\language\compose.go:23:26: undefined: Tag
........\pkg\mod\golang.org\x\text#v0.3.7\internal\language\compose.go:56:28: undefined: Tag
........\pkg\mod\golang.org\x\text#v0.3.7\internal\language\coverage.go:9:24: undefined: Language
........\pkg\mod\golang.org\x\text#v0.3.7\internal\language\match.go:17:10: undefined: Tag
........\pkg\mod\golang.org\x\text#v0.3.7\internal\language\match.go:17:35: undefined: Language
........\pkg\mod\golang.org\x\text#v0.3.7\internal\language\match.go:23:10: undefined: Tag
........\pkg\mod\golang.org\x\text#v0.3.7\internal\language\match.go:23:37: undefined: Script
........\pkg\mod\golang.org\x\text#v0.3.7\internal\language\match.go:23:37: too many errors"
I tried to solve this by upgrading to the latest version of Golang. Not sure if that made things worse.
I am using the encoding/csv package. My IDE is Visual Sudio Code. Here is my code:
package main
import (
"encoding/csv" // Problem seems to be with this package
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/gocolly/colly"
)
func main() {
fName := "FOOT-live-data.csv"
file, err := os.OpenFile(fName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("No se pudo crear archivo, error: %q", err)
return
}
defer file.Close()
writer := csv.NewWriter(file) // Package encoding/csv is used here
defer writer.Flush()
liga := ""
start := time.Now()
cons := 0
eventodds := make([]string, 3, 5)
var data [][]string
c := colly.NewCollector()
c.OnHTML(`div#USInplay-tab-FOOT div.table-row.row-wrap`, func(e *colly.HTMLElement) {
if e.DOM.Children().Length() == 1 {
if liga != e.ChildText(`div.ev-type-header`) {
liga = e.ChildText(`div.ev-type-header`)
}
} else {
cons = cons + 1
timestamp := time.Now().Round(time.Second)
eventid := e.Attr("data-mkt_id")
eventinfo := strings.Split(e.ChildText(`div[class="team-score"]`), "\n")
eventtime := e.ChildText(`span.clock_mode_forward`)
eventperiod := e.ChildText(`span.period`)
e.ForEach(`div.inplay td.seln`, func(i int, elem *colly.HTMLElement) {
eventodds[i] = elem.ChildText(`span.dec`)
})
data = append(data, []string{
timestamp.Format(time.RubyDate),
"WPlay",
liga,
eventid,
eventinfo[1] + " - " + eventinfo[4],
eventinfo[0] + "-" + eventinfo[3],
eventtime,
eventperiod,
eventodds[0],
eventodds[1],
eventodds[2],
})
}
})
c.Visit("https://apuestas.wplay.co/es")
writer.WriteAll(data)
log.Printf("Scraping finished, check file %q for results\n", fName)
secs := time.Since(start).Seconds()
fmt.Printf("Tiempo total: %v segs", secs)
}
I managed to figure out this odd situation! I debated between deleting my stupid question, or answer it myself to help someone else in a similar situation. I decided to bite the public derision bullet and answer it myself.
The problem was indeed related to a broken dependency, but not to the encoding package. The root cause of the problem is my clumsy use of Github.
I work mostly on this computer, and I have created a Github rep to save my work and to replicate it in two computers more, one laptop for the road and an old Mac.
Surely enough a couple of weeks ago I was on a business trip and worked on my project one night. I had problems with the Colly package on THAT machine, so I deleted it from the src and pkg folders, which are in sync with the Github rep. Then, mechanically and "neuronlessly", I synced my work with Github as I always do at the end of my journey.
Back to my usual computer, I did perform some more work, and did sync with my Guthub rep. When I got the warning of not "pushing" my work before "pulling" or ignoring latest version from the Github rep, I did not give it a second thought before "pulling" from the rep first.
Boom! The deleted Colly folder from my on-the-road laptop made it all the way into this machine, via Github. It made a mess on my Colly folder on this machine that I am still trying to untangle.
I also realized that there is no point on syncing the bin, pkg and src folders with Github, since only the latter contains the actual code you care for, and the other two are populated locally if content doesn't exist; they are specific to the machine and architecture you are working on. Which I am now almost certain created the problem on my laptop on the first place.
This confirms the saying that you learn more from your mistakes than your successes!
Again, I apologize for having posted the question without exhausting every last resource. I hope the conclusion of it is helpful.

main.go not able to find the package

Actually I want to use c4 to generate the c4 id for the video files, So I found the below repo which is developed to do this thing, So I clone this repo
https://github.com/Avalanche-io/c4/tree/v0.7.0
Now as suggested in this answer from stack overflow: Not able to install cmd version of c4 from github
I execute the following command in my ubuntu terminal
go get github.com/Avalanche-io
go get github.com/Avalanche-io/c4/id
go get github.com/Avalanche-io/c4/cmd/c4
then as they have shown in the example of how to use this repo
package main
import (
"fmt"
"io"
"os"
c4 "github.com/avalanche-io/c4/id"
)
func main() {
file := "main.go"
f, err := os.Open(file)
if err != nil {
panic(err)
}
defer f.Close()
// create a ID encoder.
e := c4.NewEncoder()
// the encoder is an io.Writer
_, err = io.Copy(e, f)
if err != nil {
panic(err)
}
// ID will return a *c4.ID.
// Be sure to be done writing bytes before calling ID()
id := e.ID()
// use the *c4.ID String method to get the c4id string
fmt.Printf("C4id of \"%s\": %s\n", file, id)
return
}
I just copy this same example and created a main.go file and when I run this command which they have defined here in their README.md https://github.com/Avalanche-io/c4/blob/v0.7.0/id/README.md
The command is go run main.go ```` Instead of getting the c4 id``` of the file as they have shown in their example. I am getting the following error
main.go:8:3: cannot find package "github.com/avalanche-io/c4/id" in any of:
/usr/lib/go-1.13/src/github.com/avalanche-io/c4/id (from $GOROOT)
/home/vinay/go/src/github.com/avalanche-io/c4/id (from $GOPATH)
I don't know about go language so it is becoming very difficult for me to solve the problem here, Is there any go developer which will help me out.
main.go file is not able to find the package github.com/avalanche-io/c4/id inside /home/vinay/go/src/github.com/avalanche-io/c4/id , As I can see you have run the following go get commands
go get github.com/Avalanche-io
go get github.com/Avalanche-io/c4/id
go get github.com/Avalanche-io/c4/cmd/c4
but none of them has name github.com/avalanche-io/c4/id
so according to me, You need to execute the following command
go get github.com/avalanche-io/c4/id
Now just run your main.go
go run main.go

Can't seem to get started with Go and Echo

Trying to build a simple crud api with Golang and Echo and I can't get past the first tep in the Echo docs.
I run go get -u github.com/labstack/echo/...
then I create server.go:
package main
import (
"net/http"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
}
but when I try to run: go run server.go
I get this error:
server.go:6:2: cannot find package "github.com/labstack/echo/v4" in any of:
/usr/local/Cellar/go/1.14.4/libexec/src/github.com/labstack/echo/v4 (from $GOROOT)
/Users/dariusgoore/go/src/github.com/labstack/echo/v4 (from $GOPATH)
You need to enable GO111MODULE. To enable the module you need to run this command.
export GO111MODULE=on
After enabling it when you will run go run server.go then it will install the packages again after that the program will run as expected.
I get the same issue when I run go get before go mod init. Using the following commands, I can run the server successfully:
go mod init example.com/try-echo
go get
go run server.go
I just created a new project with the same main.go and it ran without any issue. I have listed the steps I followed.
package main
import (
"net/http"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
}
and used go.mod for dependencies downloads
go mod init
go get
go run main.go
and it ran without any error
____ __
/ __/___/ / ___
/ _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.1.16
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
O\
⇨ http server started on [::]:1323
I hope this helps. If it doesn't you can run go env and share the result of the command, which will help to debug further.
export GO111MODULE=on
go mod init
go mod tidy
if not succed, add more command:
go mod download

Compile error when using Echo

I made a simple work using github.com/labstack/echo package while running I got this error.
github.com\labstack\echo\echo.go:624: e.AutoTLSManager.HTTPHandler
undefined (type autocert.Manager has no field or method HTTPHandler
package main
import "github.com/labstack/echo"
func main() {
e := echo.New()
e.Start(":")
}
This issue is addressed in issue #1082:
You seem to have a stale acme/autocert library. autocert.Manager DOES currently have that method: https://godoc.org/golang.org/x/crypto/acme/autocert#Manager.HTTPHandler I suggest a go get -u golang.org/x/crypto/acme/....

Golang on OpenShift, I can't find local module

I want to use an OpenShift test environment for my Golang applications.
I made a test application:
myproj/
------web.go
------/mylib/
-------------mylib.go
web.go is standard OpenShift file:
package main
import (
"fmt"
"net/http"
"os"
"runtime"
"./mylib"
)
func main() {
http.HandleFunc("/", hello)
bind := fmt.Sprintf("%s:%s", os.Getenv("HOST"), os.Getenv("PORT"))
fmt.Printf("listening on %s...", bind)
err := http.ListenAndServe(bind, nil)
if err != nil {
panic(err)
}
}
func hello(res http.ResponseWriter, req *http.Request) {
str := mylib.Lib();
fmt.Fprintf(res, "hello, %s from %s", str, runtime.Version())
}
and I created "mylib"
package mylib
func Lib() string {
return "world"
}
and when I run "go run web.go" everything works fine on my local computer. But when I try to upload this code to OpenShift I get the following error:
remote: -----> Using Go 1.1.2
remote: -----> Running: go get -tags openshift ./...
remote: can't load package: /var/lib/openshift/5354e6fd4382ec2dca000223/app-root/runtime/repo/.openshift/g/src/github.com/smarterclayton/goexample/web.go:8:2: local import "./mylib" in non-local package
remote: An error occurred executing 'gear postreceive' (exit code: 1)
remote: Error message: CLIENT_ERROR: Failed to execute: 'control build' for /var/lib/openshift/5354e6fd4382ec2dca000223/go
What does this mean? Why can't Golang find this package? I can't write all code in one file. How should I write the application for OpenShift?
I know this question is old but i had the same problem and it was difficult to find the solution, so i decided to ask in order to help who will run on the same problem.
The solution is very simple and can be found in readme of the go cartdrige repo on github: github.com/smarterclayton/openshift-go-cart
You have to create a file named .godir and put here the name of the main package of your server.
For example if you put myserver you can use:
package main
import "myserver/mylib"
func main() {
mylib.DoStuff()
}
Basically when you push on openshift the repo is copied in the directory placed in .godir before the build

Resources