Episode 4 - Types
Tags: go, showdev, tutorial
Published: 2019-09-11 08:05:17

:camera: Photo by [Amador Loureiro](https://unsplash.com/photos/BVyNlchWqzs) on Unsplash

Hey we are getting closer to being able to use dev.to API.

### What is a type

According to [this article](https://thenewstack.io/understanding-golang-type-system/) that is the definition I find easier to understand

> The type system is the most important feature of a programming language, letting you organize your application data. Go follows a minimalist approach for its type system. It provides several built-in types such as string, bool, int and float64.

You can think of a type like described there with primitives, this describes the space in memory needed and the type of data that is going to be allocated there.

For example for a type user:

```go
// User contains information about a devto account
type User struct {
    Name            string  `json:"name,omitempty"`
    Username        string  `json:"username,omitempty"`
    TwitterUsername string  `json:"twitter_username,omitempty"`
    GithubUsername  string  `json:"github_username,omitempty"`
    WebsiteURL      *WebURL `json:"website_url,omitempty"`
    ProfileImage    *WebURL `json:"profile_image,omitempty"`
    ProfileImage90  *WebURL `json:"profile_image_90,omitempty"`
}

```

We are telling the go compiler that every time something has the `devto.User` type assigned it should allocate enough space in memory to place an object with this structure.

Here you have an amazing repository with a lot of [go examples](https://github.com/golang/example/tree/master/gotypes#types)

### WTF are those things after the type?

Those are called tags, and for me it's kinda the go way of doing annotations, coming from PHP at least this is how it looks like.

The Go spec in the Struct types definition defines tags as:

> A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. An empty tag string is equivalent to an absent tag. The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored.

This will helps us to do something called JSON unmarshalling, but hold your horses, we are not there yet!

### The practical dev types

For getting the types you need to analyze the example responses from the API documentation, we will use the [Get article spec](https://docs.dev.to/api/#tag/articles/paths/~1articles~1{id}/get) because for the create request we have only the parameters we are supposed to submit, in the get article endpoint you get the article information plus all the metadata devto is adding in the background.

Now nesting structs when creating types is not a road I will recommend you to take, because it gets messy quite fast, instead try to start breaking every nested object to its own type.

So you can check the types I've come up with in the project repository, I also added some custom unmarshalling for the URLs.

Here I will just leave the `devto.Article` type, which is the main dev.to type.

```go
// Article contains all the information related to a single
// information resource from devto.
type Article struct {
	TypeOf                 string       `json:"type_of,omitempty"`
	ID                     uint32       `json:"id,omitempty"`
	Title                  string       `json:"title,omitempty"`
	Description            string       `json:"description,omitempty"`
	CoverImage             *WebURL      `json:"cover_image,omitempty"`
	SocialImage            *WebURL      `json:"social_image,omitempty"`
	PublishedAt            *time.Time   `json:"published_at,omitempty"`
	EditedAt               *time.Time   `json:"edited_at,omitempty"`
	CrossPostedAt          *time.Time   `json:"crossposted_at,omitempty"`
	LastCommentAt          *time.Time   `json:"last_comment_at,omitempty"`
	TagList                Tags         `json:"tag_list,omitempty"`
	Tags                   string       `json:"tags,omitempty"`
	Slug                   string       `json:"slug,omitempty"`
	Path                   *WebURL      `json:"path,omitempty"`
	URL                    *WebURL      `json:"url,omitempty"`
	CanonicalURL           *WebURL      `json:"canonical_url,omitempty"`
	CommentsCount          uint         `json:"comments_count,omitempty"`
	PositiveReactionsCount uint         `json:"positive_reactions_count,omitempty"`
	PublishedTimestamp     *time.Time   `json:"published_timestamp,omitempty"`
	User                   User         `json:"user,omitempty"`
	Organization           Organization `json:"organization,omitempty"`
	BodyHTML               string       `json:"body_html,omitempty"`
	BodyMarkdown           string       `json:"body_markdown,omitempty"`
	Published              bool         `json:"published,omitempty"`
}

```

Take a look raise your questions and contribute if you feel like something is not right.

{% github https://github.com/VictorAvelar/devto-api-go/ %}