# Distributed Services with Go: Chapter 1 Suggestions

**URL:** <https://forum.devtalk.com/t/distributed-services-with-go-chapter-1-suggestions/6979>\
**Category:** PragProg Customers\
**Tags:** book-distributed-services-with-go, errata\
**Created:** [13 February 2021 15:54 UTC](https://forum.devtalk.com/t/distributed-services-with-go-chapter-1-suggestions/6979 "2021-02-13T15:54:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raul](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@raul](https://forum.devtalk.com/u/raul)\
**Post date:** [13 February 2021 15:54 UTC](https://forum.devtalk.com/t/distributed-services-with-go-chapter-1-suggestions/6979/1 "2021-02-13T15:54:40Z")

</div>

Hi Travis! Thank you for the cool book! 🙂

I made a list of issues and thought I could post them chapter by chapter. I’m reviewing distributed-services-with-go\_B4.0.pdf. Please let me know if you’d prefer me to post them issue by issue instead. That’s going to take longer…

Chapter 1. Let’s Go

Page footer includes links to:  
+ report erratum: [http://pragprog.com/titles/tjgo/errata/add](http://pragprog.com/titles/tjgo/errata/add)  
+ discuss: [http://forums.pragprog.com/forums/tjgo](http://forums.pragprog.com/forums/tjgo)  
+ Are both of these outdated?

So you can build a simple commit log with a slice.

- (Before reading up on Go) What’s a slice?
- For a Go newbie, maybe it’s better to call it ‘Go slice’? But I guess the book title does say ‘with Go’ 🙂

Set Up the Project  
$ mkdir proglog  
$ cd proglog  
$ go mod init [github.com/travisjeffery/proglog](http://github.com/travisjeffery/proglog)

- This creates a go.mod in the top-level proglog folder
- But when I checkout the github project to compare, the go.mod file is in proglog/LetsGo. Thought I’d mention in case things aren’t as intended.

Create a internal/server directory tree in the root of your project and put the following code under the server directory in a file called log.go:

- Create a → Create ‘an’
- LetsGo in the code path is not introduced anywhere?
- Does it need to be there, or should it be ‘proglog’ instead?
- Personally, I think Chapter\_1/ would make it clearer if that were the intention.

LetsGo/internal/server/log.go

- Trying to copy the code from the pdf didn’t work - all kinds of extra formatting was turning up on the Mac? Is there some option I need to be enabling? I finally copied in one line at a time because even copying one method at a time ran into issues.
- Clicking on the link takes to the code in the browser! Hadn’t known that, maybe you could mention it in the text? That was a life saver in terms of helping with copying in code!

1. Unmarshal the request’s JSON body into a struct.

- (Before reading up on Go) Does struct mean a record here? What’s a struct?
- For a Go newbie, maybe it’s better to call it ‘Go struct’?

so the user just needs to call ListenAndServe() to

- What’s ListenAndServe()? Sorry didn’t understand this

$ go run main.go

- In which folder please?

- From the top folder, I get: “stat main.go: no such file or directory”

- I ran it successfully from cmd/server but maybe it is worth mentioning explicitly?

- First run, I got:  
go: finding [github.com/travisjeffery/proglog/internal/server](http://github.com/travisjeffery/proglog/internal/server) latest  
go: finding [github.com/travisjeffery/proglog/internal](http://github.com/travisjeffery/proglog/internal) latest  
build command-line-arguments: cannot load [github.com/travisjeffery/proglog/internal/server:](http://github.com/travisjeffery/proglog/internal/server:) no matching versions for query “latest”

- Fixed it with:  
— main.go.old 2021-02-13 15:39:21.000000000 +0530  
+++ main.go 2021-02-13 15:39:32.000000000 +0530  
@@ -3,7 +3,7 @@  
import (  
“log”

- “[github.com/travisjeffery/proglog/internal/server](http://github.com/travisjeffery/proglog/internal/server)”

- “[github.com/travisjeffery/proglog/LetsGo/internal/server](http://github.com/travisjeffery/proglog/LetsGo/internal/server)”  
)

func main() {

curl -X POST localhost:8080 -d   
‘{“record”: {“value”: “TGV0J3MgR28gIzEK”}}’

- That worked!
- Would it be a good idea to include the output as well, so that people can cross-check whether things look good?
- {“offset”:0}

curl -X GET localhost:8080 -d ‘{“offset”: 0}’

- Would it be a good idea to include the output folks should expect?
- {“record”:{“value”:“TGV0J3MgR28gIzEK”,“offset”:0}}

Would it be a good idea to include a section on setting up GoLand that would help with debugging later on, even in a later chapter?

- It took me a while to figure out what to point GOROOT at, with goenv. I finally pointed it at the go version in ~/.goenv/versions/ that my go was coming from and am not sure whether that’s the way to go but it seems to work for now.

---

<div class="post-metadata">

**Author:** ![ambareeshsrja16](https://forum.devtalk.com/user_avatar/forum.devtalk.com/ambareeshsrja16/32/3465_2.png) [@ambareeshsrja16](https://forum.devtalk.com/u/ambareeshsrja16)\
**Post date:** [6 October 2021 17:13 UTC](https://forum.devtalk.com/t/distributed-services-with-go-chapter-1-suggestions/6979/2 "2021-10-06T17:13:43Z")

</div>

Also, I was wondering, for the log.go program (the very first one), is it necessary to lock the mutex for Read()? Can’t you have multiple threads reading concurrently? I don’t think the Read function is a critical section.

---

<div class="post-metadata">

**Author:** ![rsippl](https://forum.devtalk.com/user_avatar/forum.devtalk.com/rsippl/32/3556_2.png) [@rsippl](https://forum.devtalk.com/u/rsippl)\
**Post date:** [3 November 2021 16:59 UTC](https://forum.devtalk.com/t/distributed-services-with-go-chapter-1-suggestions/6979/3 "2021-11-03T16:59:17Z")

</div>

The mutex makes sure you don’t read from the log while writing to it. You could have n concurrent readers without needing a mutex, if only you were sure no one is writing to it. Actually, this is a scenario for sync.RWMutex. Its docs explain it well: “The lock can be held by an arbitrary number of readers or a single writer”.

---

<div class="post-metadata">

**Author:** ![ambareeshsrja16](https://forum.devtalk.com/user_avatar/forum.devtalk.com/ambareeshsrja16/32/3465_2.png) [@ambareeshsrja16](https://forum.devtalk.com/u/ambareeshsrja16)\
**Post date:** [5 November 2021 17:53 UTC](https://forum.devtalk.com/t/distributed-services-with-go-chapter-1-suggestions/6979/4 "2021-11-05T17:53:55Z")

</div>

That makes sense, thank you!
