PacktPublishing / Hands-On-Restful-Web-services-with-Go

Hands-On Restful Web services with Go by Packt Publishing
MIT License
107 stars 73 forks source link

Chapter 11 (and many more before it) examples simply don't work. #27

Open pnunn opened 3 years ago

pnunn commented 3 years ago

Did you guys do ANY technical editing before releasing this book?

As as new comer to go, debugging your crappy examples has been quite a learning experience, but I'm now stuck on the chapter 11 code with the micro service, which is sad, because it really interesting stuff.

go build 14:38:28 main.go:6:2: cannot find package "github.com/micro/go-micro" in any of: /usr/local/go/src/github.com/micro/go-micro (from $GOROOT) /home/pnunn/go/src/github.com/micro/go-micro (from $GOPATH) proto/encryption.pb.micro.go:14:2: cannot find package "github.com/micro/go-micro/v2/api" in any of: /usr/local/go/src/github.com/micro/go-micro/v2/api (from $GOROOT) /home/pnunn/go/src/github.com/micro/go-micro/v2/api (from $GOPATH) proto/encryption.pb.micro.go:15:2: cannot find package "github.com/micro/go-micro/v2/client" in any of: /usr/local/go/src/github.com/micro/go-micro/v2/client (from $GOROOT) /home/pnunn/go/src/github.com/micro/go-micro/v2/client (from $GOPATH) proto/encryption.pb.micro.go:16:2: cannot find package "github.com/micro/go-micro/v2/server" in any of: /usr/local/go/src/github.com/micro/go-micro/v2/server (from $GOROOT) /home/pnunn/go/src/github.com/micro/go-micro/v2/server (from $GOPATH)

I have go done go get -v github.com/micro/micro/v2 (and v3 because that's what the documentation seems to suggest) to no avail this time.

What do I need to do to make this work?

This is SERIOUSLY disappointing because the content of the books is really good and interesting, but SO badly let down by the technical errors.

narenaryan commented 3 years ago

@pnunn, I am really sorry about your experience with the code samples. Can I know which operating system you are using and what are values for the below environment variables?

  1. GOPATH
  2. GOROOT
pnunn commented 3 years ago

Happy new year @narenaryan. Now with more go experience, I'm coming back to your book.

I'm back into chapter 11 but am still not getting a compile.

GOPATH and GOROOT, if I echo $THEM I get blanks.

If I however run go env I get the following.

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/pnunn/.cache/go-build"
GOENV="/home/pnunn/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/pnunn/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/pnunn/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/pnunn/go/src/github.com/pnunn/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build226749192=/tmp/go-build -gno-record-gcc-switches"

Which all seem to be correct, the code is is /home/pnunn/go/src/github.com/pnunn/chapter11 in this case.

I'll keep trying to work this out. At the moment I can't even find utils in the encryptString main.go

go run main.go                                                                                         
go: finding module for package github.com/pnunn/chapter11/encryptString/utils

and it goes off looking in git which fails obviously.

pnunn commented 3 years ago

OK, using modules has got me further. I've got the strings version running, but when I try and build the service I get

 go build  
# github.com/coreos/etcd/clientv3/balancer/picker
/home/pnunn/go/pkg/mod/github.com/coreos/etcd@v3.3.17+incompatible/clientv3/balancer/picker/err.go:37:44: undefined: balancer.PickOptions
/home/pnunn/go/pkg/mod/github.com/coreos/etcd@v3.3.17+incompatible/clientv3/balancer/picker/roundrobin_balanced.go:55:54: undefined: balancer.PickOptions
# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
/home/pnunn/go/pkg/mod/github.com/coreos/etcd@v3.3.17+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:114:78: undefined: resolver.BuildOption
/home/pnunn/go/pkg/mod/github.com/coreos/etcd@v3.3.17+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:182:31: undefined: resolver.ResolveNowOption
pnunn commented 3 years ago

I tried to upgrade the code to v3 (the way to go long term), but my go fu is not up to the task...

narenaryan commented 3 years ago

@pnunn Looks like you have a problem with your git setup. I ran those examples again and they are working fine. Can you do something. If you have docker(or docker desktop) installed, can you create a Ubuntu container and test it?

pnunn commented 3 years ago

Hi @narenaryan, I will do that tomorrow. I'm running this an a ubuntu dev VM, so will give a docker environment a go. It seems a bunch of people are having problems with micro though because its been upgraded to use a beta package (or was using a beta package) and v1 and v2 are problematic. Seems the best approach is to upgrade to v3, but I can't get that to compile either.

Oh well, guess its a learning experience.

narenaryan commented 3 years ago

@pnunn I am sorry that, it is burning your valuable time. Good to know about micro upgrade. I will test it too by upgrading to latest version. Thanks.

pnunn commented 3 years ago

Its fine @narenaryan, its all good learning... :)

For what its worth, this is what happens with v3


 go build                                                                                              
# github.com/pnunn/chapter11/encryptService
./main.go:22:47: cannot use service.Server() (type "github.com/micro/micro/v3/service/server".Server) as type "github.com/micro/go-micro/server".Server in argument to encryption.RegisterEncrypterHandler:
        "github.com/micro/micro/v3/service/server".Server does not implement "github.com/micro/go-micro/server".Server (wrong type for Handle method)
                have Handle("github.com/micro/micro/v3/service/server".Handler) error
                want Handle("github.com/micro/go-micro/server".Handler) error

main.go looks like

package main

import (
        fmt "fmt"

        //micro "github.com/micro/go-micro/v2"
        micro "github.com/micro/micro/v3/service"
        proto "github.com/pnunn/chapter11/encryptService/proto"
)

func main() {
        // Create a new service. Optionally include some options here.
        //service := micro.NewService(
        service := micro.New(
                micro.Name("encrypter"),
        )

        // Init will parse the command line flags.
        service.Init()

        // Register handler
        proto.RegisterEncrypterHandler(service.Server(), new(Encrypter))

        // Run the server
        if err := service.Run(); err != nil {
                fmt.Println(err)
        }
}
epicavic commented 3 years ago

https://github.com/asim/go-micro#getting-started new go-micro version

main.go:
import "github.com/asim/go-micro/v3"

https://github.com/asim/go-micro#code-generation new go-micro version relies on new version of protoc-gen-micro proto/encryption.pb.micro.go should be regenerated

$ go get github.com/asim/go-micro/cmd/protoc-gen-micro/v3
$ protoc -I=. --micro_out=. --go_out=. proto/encryption.proto