Embed Notice
HTML Code
Corresponding Notice
- Embed this notice
ℜ𝔨𝔎 (rkk@eientei.org)'s status on Friday, 12-Jul-2024 10:12:44 JSTℜ𝔨𝔎 @kirby
At first glance, it seems to be true, with the code written in the C programming language seeming to be very complicated[1]:
```C
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
/* In windows, this inits the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be an https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
/* Perform the request, res gets the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
```
How about trimming all the comments and code that is common for most of programs written in C? Also, we can omit error handling, etc... Now it doesn't look that difficult:
```C
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_global_cleanup();
```
Now there are only 7 LoC:
+ 2 LoC are required to create curl handle and initialize it.
+ 2 LoC are needed to clean everything.
The actual HTTP request performing code is just 3 LoC, two of which set up arguments and one executes the request.
On the other hand, the example written in Go:
+ lacks error handling;
+ uses magic constants;
+ forces one to use GC;
Should you write your code snippet with safety and good codestyle in consideration, your example would be just as verbous as the C one, except for the 4 LoC required to initialize and clean up the library.
[1]: https://curl.se/libcurl/c/http-post.html