DEV Community

Cover image for It's Been Three Months Since My First Go LoC 🤓 🎓

It's Been Three Months Since My First Go LoC 🤓 🎓

Boris Jamot ✊ / on December 22, 2018

It's been 3 months since I wrote my first Line of Code in Golang. My Journey from PHP to Go Boris Jam...
Collapse
 
rhymes profile image
rhymes

Hi Boris, happy you're liking Go so far!

(un)-marshalling
If someone knows a better solution, don't hesitate!

I think the best solution is validation. I used govalidator successfully in a project.

You can do stuff like:

type Ticket struct {
  Id        int64     `json:"id"`
  FirstName string    `json:"firstname" valid:"required"`
}

Writing unit tests is the worst part of my developer journey in Golang

It took me a while to get testing right in my first project but Go has really powerful testing chops. I'm starting to like test tables and you can easily debug test cases from VSCode.

With Go, there is no equivalent of PHPUnit or JUnit that allows to quickly mock any dependency.

testify seems nice, haven't tried it yet

A couple of posts about unit testing in Go that I read:

It seems like the official Go doc (which is great) is quite the only available resource, except posts on StackOverflow.

There are resources around, you just have to find them. A good place to start are the newsletters Golang Weekly and Master the World of Golang.

What is also confusing is that if you google something like "go + ", you won't get many results. You'll have more luck with "golang + ".

Ah ah yes. Naming it Go was a bad move, it's too late for that

Have fun!

Collapse
 
gbafana25 profile image
Gareth M.

They should've just called it Gopher, like GopherCon right?

Collapse
 
biros profile image
Boris Jamot ✊ /

I will have a look at govalidator.
I already use testify for assertions, but it's not designed for mocking.

Thanks for your support, I'll have a look at your resources!

Collapse
 
rhymes profile image
rhymes

What about this part of testify's documentation? github.com/stretchr/testify#mock-p...

It doesn't work?

Thread Thread
 
biros profile image
Boris Jamot ✊ /

I didn't know about mocking with testify. It seems to be working similarly to gomock.
The problem is that the mocks have to be generated manually and to be maintained along with your codebase.

Thread Thread
 
rhymes profile image
rhymes

Yeah, fortunately I don't use mocking that much

Collapse
 
damnjan profile image
Damnjan Jovanovic • Edited

This is the best article for Go I read because it comes from the person who is native in PHP. Awesome work Boris!
I appreciate your focus on tests; it helps me understand mocking in Go. Please continue to write about your from PHP to GO transition. I’m really curious about that.

Collapse
 
biros profile image
Boris Jamot ✊ /

Thank you so much, I'm very happy that you found it useful!

Collapse
 
damnjan profile image
Damnjan Jovanovic

It is beneficial to me, since I am also thinking to start learning Go. Not necessary to be my primary language, I first want to understand the concept.

Collapse
 
ladydascalie profile image
Benjamin Cable

how to distinguish between missing attributes and attributes with default values?

Use pointers!

Imagine the following:

type Person struct {
    Name *string
    Age  int
}

then try to unmarshal this: { "Age": 30 }

You will get this:

fmt.Printf("%#v\n", p)

> main.Person{Name:(*string)(nil), Age:30}

if a key is missing, it's a simple nil check away.

Another nice technique I like to use is as follows:

Declare an ok interface:

type ok interface {
    OK() error
}

Implement it on your type:

func (p *Person) OK() error {
    if p.Name == nil {
        return errors.New("missing field: name")
    }
    return nil
}

Then simply:

p.OK()

> 2009/11/10 23:00:00 missing field: name

You can see the full example in the go playground:

play.golang.org/p/P_88niVqOke

Collapse
 
jeromelaforge profile image
Jérôme Laforge

For me, the reading the body of request (or response in case of HTTP client) is not a big deal. You can easily define helper function that read all Body, create a new io.Reader then return read Body as bytes array.

For me (and also for Go maintener net/http Brad Fitzpatrick), HTTP client has to be improved (c.f. github.com/bradfitz/exp-httpclient) and will be.

Collapse
 
biros profile image
Boris Jamot ✊ /

Hi Jérôme, it's fun to see you here on dev.to!
What's the weather like in caen?
What you suggest with creating an io.Reader proves what I said above : many trivial things in popular languages can become complicated in Go.

Collapse
 
jeromelaforge profile image
Jérôme Laforge • Edited

What's the weather like in caen?

cloudy ...

What you suggest with creating an io.Reader proves what I said above : many trivial things in popular languages can become complicated in Go.

Maybe, but many complicated things in popular languages become trivial in Go ;) (Concurency, very good performance on tiny configuration, async task without tuning your thread pool, deploy on "From Scratch" docker's image and so on).

Collapse
 
dannypsnl profile image
林子篆

You can use pointer so missing value would be nil

Collapse
 
biros profile image
Boris Jamot ✊ /

It's a good idea, thanks!