How to write 3000 lines in one afternoon? Remember the Golang API SDK production process for a cloud

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Recently participated in a Cloud SDK writing contest, the official sample is a Python version, request directly with **params Such method constructs the request body

Because the Golang is static language, we rigorous! No mutable Parameters! There are no default values!!

So I wrote this code for one hours, like this:

func (c *Client) GetHostInstance(id string, option string, count int) {// bla....}

Every API repeated over and over, really miserable, when is the head ah, life is too short ah, early know also use Python ... So I went to bed, um, dream robpike Dreams woke up, suddenly thought, a few days ago to learn the Goreflect library, can reflect the type of the call, so I just construct a struct, and then traverse each item, can not save a lot of time? So, I began to construct a variety of structs type GetHostInstance struct{ id string option string count int } from the JSON library directly pull some code to traverse the Numfield, immediately feel their awesome!! However, after writing three I suddenly found that some parameters in the API is optional!!!

Fortunately, Rob greatly has been waiting for me to think well, that is the tagfield of the struct, so I TM also constructs a own parser

type GetHostInstance struct{ id string `cloud:"optional"` option string count int }// .....解析器部分代码// .....获取tag tag := typ.Field(i).Tag.Get("cloud")

After removing the tag value, check whether the struct is nil to determine if the parameter is passed! Finally variable parameters so the pain of the thing all let me solve, and then, I wrote 3 struct, tired not ... I'm too young to be physically tired, but I've decided to finish it because it's such a beautiful solution. What if I don't have to get a mug for a trophy? So I wrote 3 APIs, I found that I was copying and pasting the official API document, so I sacrificed my copy sticky DAFA +vim macro immediately quickly a lot, become 5 minutes an API, I looked at the document ... A total of more than 50 TM interface!! 5*50 = 250 minutes = 4 hours = I can take a look at the Black Hawk Fall + 5 episode "GTA 5 funny video"!! So I began to be bored to the official API document, I suddenly found that the official API document is written with Sphinx, just set a layer of their own CSS –> because my request body is only a struct, the official also the type of the request has been labeled well, then the ear sounded: life is too short, I use Python's slogan I can write a program in Python, the official document directly into the SDK code AH!!! So, say dry!! Find requests and other libraries, life immediately happy a lot of there is no ~ spectacular generation, even comments have ( ̄▽ ̄) hahaha

Generator Code: HTTPS://GIST.GITHUB.COM/MENGZHUO/F1B07DECB69EEA6E7DAB

Generate a good SDK:HTTPS://GITHUB.COM/MENGZHUO/UCLOUD-GO-SDK

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.