-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaddressing_cgo_pains_one_at_a_time.html
More file actions
1286 lines (1266 loc) · 129 KB
/
addressing_cgo_pains_one_at_a_time.html
File metadata and controls
1286 lines (1266 loc) · 129 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--
This file has been auto-generated by main.rs from a markdown file of the same name.
Do not edit it by hand.
-->
<!DOCTYPE html>
<html>
<head>
<title>Addressing CGO pains, one at a time</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link type="application/atom+xml" href="/blog/feed.xml" rel="self">
<link rel="shortcut icon" type="image/ico" href="/blog/favicon.ico">
<link rel="stylesheet" type="text/css" href="main.css">
<link rel="stylesheet" href="https://unpkg.com/@highlightjs/cdn-assets@11.8.0/styles/default.min.css">
<script type="module" src="main.js" async></script>
<script type="module" src="search.js" async></script>
</head>
<body>
<div id="banner">
<div id="name">
<img id="me" src="me.jpeg">
<span>Philippe Gaultier</span>
</div>
<input id="search" placeholder="🔎 Search" autocomplete=off>
<ul>
<li> <button id="dark-light-mode">Dark/Light</button> </li>
<li> <a href="/blog/body_of_work.html">Body of work</a> </li>
<li> <a href="/blog/articles-by-tag.html">Tags</a> </li>
<li> <a href="https://github.com/gaultier/resume/raw/master/Philippe_Gaultier_resume_en.pdf">
Resume
</a> </li>
<li> <a href="/blog/feed.xml">
<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M5.5 3.5C4.39543 3.5 3.5 4.39543 3.5 5.5V18.5C3.5 19.6046 4.39543 20.5 5.5 20.5H18.5C19.6046 20.5 20.5 19.6046 20.5 18.5V5.5C20.5 4.39543 19.6046 3.5 18.5 3.5H5.5ZM7 19C8.10457 19 9 18.1046 9 17C9 15.8954 8.10457 15 7 15C5.89543 15 5 15.8954 5 17C5 18.1046 5.89543 19 7 19ZM6.14863 10.5052C6.14863 10.0379 6.52746 9.65906 6.99478 9.65906C7.95949 9.65906 8.91476 9.84908 9.80603 10.2183C10.6973 10.5874 11.5071 11.1285 12.1893 11.8107C12.8715 12.4929 13.4126 13.3027 13.7817 14.194C14.1509 15.0852 14.3409 16.0405 14.3409 17.0052C14.3409 17.4725 13.9621 17.8514 13.4948 17.8514C13.0275 17.8514 12.6486 17.4725 12.6486 17.0052C12.6486 16.2627 12.5024 15.5275 12.2183 14.8416C11.9341 14.1556 11.5177 13.5324 10.9927 13.0073C10.4676 12.4823 9.84437 12.0659 9.15842 11.7817C8.47246 11.4976 7.73726 11.3514 6.99478 11.3514C6.52746 11.3514 6.14863 10.9725 6.14863 10.5052ZM7 5.15385C6.53268 5.15385 6.15385 5.53268 6.15385 6C6.15385 6.46732 6.53268 6.84615 7 6.84615C8.33342 6.84615 9.65379 7.10879 10.8857 7.61907C12.1176 8.12935 13.237 8.87728 14.1799 9.82015C15.1227 10.763 15.8707 11.8824 16.3809 13.1143C16.8912 14.3462 17.1538 15.6666 17.1538 17C17.1538 17.4673 17.5327 17.8462 18 17.8462C18.4673 17.8462 18.8462 17.4673 18.8462 17C18.8462 15.4443 18.5397 13.9039 17.9444 12.4667C17.3491 11.0294 16.4765 9.72352 15.3765 8.6235C14.2765 7.52349 12.9706 6.65091 11.5333 6.05558C10.0961 5.46026 8.55566 5.15385 7 5.15385Z" fill="currentColor"/>
</svg>
</a> </li>
<li> <a href="https://www.linkedin.com/in/philippegaultier/">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" data-supported-dps="24x24" fill="currentColor" width="24" height="24" focusable="false">
<path d="M20.5 2h-17A1.5 1.5 0 002 3.5v17A1.5 1.5 0 003.5 22h17a1.5 1.5 0 001.5-1.5v-17A1.5 1.5 0 0020.5 2zM8 19H5v-9h3zM6.5 8.25A1.75 1.75 0 118.3 6.5a1.78 1.78 0 01-1.8 1.75zM19 19h-3v-4.74c0-1.42-.6-1.93-1.38-1.93A1.74 1.74 0 0013 14.19a.66.66 0 000 .14V19h-3v-9h2.9v1.3a3.11 3.11 0 012.7-1.4c1.55 0 3.36.86 3.36 3.66z"/>
</svg>
</a> </li>
<li> <a href="https://github.com/gaultier">
<svg height="32" aria-hidden="true" viewBox="0 0 24 24" version="1.1" width="32" data-view-component="true" fill="currentColor">
<path d="M12.5.75C6.146.75 1 5.896 1 12.25c0 5.089 3.292 9.387 7.863 10.91.575.101.79-.244.79-.546 0-.273-.014-1.178-.014-2.142-2.889.532-3.636-.704-3.866-1.35-.13-.331-.69-1.352-1.18-1.625-.402-.216-.977-.748-.014-.762.906-.014 1.553.834 1.769 1.179 1.035 1.74 2.688 1.25 3.349.948.1-.747.402-1.25.733-1.538-2.559-.287-5.232-1.279-5.232-5.678 0-1.25.445-2.285 1.178-3.09-.115-.288-.517-1.467.115-3.048 0 0 .963-.302 3.163 1.179.92-.259 1.897-.388 2.875-.388.977 0 1.955.13 2.875.388 2.2-1.495 3.162-1.179 3.162-1.179.633 1.581.23 2.76.115 3.048.733.805 1.179 1.825 1.179 3.09 0 4.413-2.688 5.39-5.247 5.678.417.36.776 1.05.776 2.128 0 1.538-.014 2.774-.014 3.162 0 .302.216.662.79.547C20.709 21.637 24 17.324 24 12.25 24 5.896 18.854.75 12.5.75Z"/>
</svg>
</a> </li>
<li> <a href="https://hachyderm.io/@pg">
<svg width="75" height="79" viewBox="0 0 75 79" xmlns="http://www.w3.org/2000/svg" fill="currentColor">
<path d="M73.8393 17.4898C72.6973 9.00165 65.2994 2.31235 56.5296 1.01614C55.05 0.797115 49.4441 0 36.4582 0H36.3612C23.3717 0 20.585 0.797115 19.1054 1.01614C10.5798 2.27644 2.79399 8.28712 0.904997 16.8758C-0.00358524 21.1056 -0.100549 25.7949 0.0682394 30.0965C0.308852 36.2651 0.355538 42.423 0.91577 48.5665C1.30307 52.6474 1.97872 56.6957 2.93763 60.6812C4.73325 68.042 12.0019 74.1676 19.1233 76.6666C26.7478 79.2728 34.9474 79.7055 42.8039 77.9162C43.6682 77.7151 44.5217 77.4817 45.3645 77.216C47.275 76.6092 49.5123 75.9305 51.1571 74.7385C51.1797 74.7217 51.1982 74.7001 51.2112 74.6753C51.2243 74.6504 51.2316 74.6229 51.2325 74.5948V68.6416C51.2321 68.6154 51.2259 68.5896 51.2142 68.5661C51.2025 68.5426 51.1858 68.522 51.1651 68.5058C51.1444 68.4896 51.1204 68.4783 51.0948 68.4726C51.0692 68.4669 51.0426 68.467 51.0171 68.4729C45.9835 69.675 40.8254 70.2777 35.6502 70.2682C26.7439 70.2682 24.3486 66.042 23.6626 64.2826C23.1113 62.762 22.7612 61.1759 22.6212 59.5646C22.6197 59.5375 22.6247 59.5105 22.6357 59.4857C22.6466 59.4609 22.6633 59.4391 22.6843 59.422C22.7053 59.4048 22.73 59.3929 22.7565 59.3871C22.783 59.3813 22.8104 59.3818 22.8367 59.3886C27.7864 60.5826 32.8604 61.1853 37.9522 61.1839C39.1768 61.1839 40.3978 61.1839 41.6224 61.1516C46.7435 61.008 52.1411 60.7459 57.1796 59.7621C57.3053 59.7369 57.431 59.7154 57.5387 59.6831C65.4861 58.157 73.0493 53.3672 73.8178 41.2381C73.8465 40.7606 73.9184 36.2364 73.9184 35.7409C73.9219 34.0569 74.4606 23.7949 73.8393 17.4898Z"/>
<path d="M61.2484 27.0263V48.114H52.8916V27.6475C52.8916 23.3388 51.096 21.1413 47.4437 21.1413C43.4287 21.1413 41.4177 23.7409 41.4177 28.8755V40.0782H33.1111V28.8755C33.1111 23.7409 31.0965 21.1413 27.0815 21.1413C23.4507 21.1413 21.6371 23.3388 21.6371 27.6475V48.114H13.2839V27.0263C13.2839 22.7176 14.384 19.2946 16.5843 16.7572C18.8539 14.2258 21.8311 12.926 25.5264 12.926C29.8036 12.926 33.0357 14.5705 35.1905 17.8559L37.2698 21.346L39.3527 17.8559C41.5074 14.5705 44.7395 12.926 49.0095 12.926C52.7013 12.926 55.6784 14.2258 57.9553 16.7572C60.1531 19.2922 61.2508 22.7152 61.2484 27.0263Z" fill="#928374" />
<defs>
<linearGradient id="paint0_linear_549_34" x1="37.0692" y1="0" x2="37.0692" y2="79" gradientUnits="userSpaceOnUse">
<stop stop-color="#6364FF"/>
<stop offset="1" stop-color="#563ACC"/>
</linearGradient>
</defs>
</svg>
</a> </li>
<li> <a href="https://bsky.app/profile/pgaultier.bsky.social">
<svg fill="currentColor" viewBox="0 0 64 57" width="32" style="width: 32px; height: 28.5px;"><path d="M13.873 3.805C21.21 9.332 29.103 20.537 32 26.55v15.882c0-.338-.13.044-.41.867-1.512 4.456-7.418 21.847-20.923 7.944-7.111-7.32-3.819-14.64 9.125-16.85-7.405 1.264-15.73-.825-18.014-9.015C1.12 23.022 0 8.51 0 6.55 0-3.268 8.579-.182 13.873 3.805ZM50.127 3.805C42.79 9.332 34.897 20.537 32 26.55v15.882c0-.338.13.044.41.867 1.512 4.456 7.418 21.847 20.923 7.944 7.111-7.32 3.819-14.64-9.125-16.85 7.405 1.264 15.73-.825 18.014-9.015C62.88 23.022 64 8.51 64 6.55c0-9.818-8.578-6.732-13.873-2.745Z"/></svg>
</a> </li>
</ul>
</div>
<div id="search-matches" hidden>
</div>
<div id="pseudo-body">
<div class="article-prelude">
<p><a href="/blog"> ⏴ Back to all articles</a></p>
<p class="publication-date">Published on 2025-02-14. Last modified on 2026-03-08.</p>
</div>
<div class="article-title">
<h1>Addressing CGO pains, one at a time</h1>
<div class="tags"> <a href="/blog/articles-by-tag.html#go" class="tag">Go</a> <a href="/blog/articles-by-tag.html#c" class="tag">C</a> <a href="/blog/articles-by-tag.html#rust" class="tag">Rust</a> <a href="/blog/articles-by-tag.html#zig" class="tag">Zig</a> <a href="/blog/articles-by-tag.html#docker" class="tag">Docker</a> <a href="/blog/articles-by-tag.html#cross-compilation" class="tag">Cross-compilation</a> <a href="/blog/articles-by-tag.html#musl" class="tag">Musl</a> </div>
</div>
<details class="toc"><summary>Table of contents</summary>
<ul>
<li>
<a href="#cgo-does-not-have-unions">CGO does not have unions</a>
</li>
<li>
<a href="#slices-vs-strings">Slices vs Strings</a>
</li>
<li>
<a href="#test-a-c-function-in-go-tests">Test a C function in Go tests</a>
</li>
<li>
<a href="#the-go-compiler-does-not-detect-changes">The Go compiler does not detect changes</a>
</li>
<li>
<a href="#false-positive-warnings">False positive warnings</a>
</li>
<li>
<a href="#white-space-is-significant">White space is significant</a>
</li>
<li>
<a href="#debug-go-and-c-rust">Debug Go and C/Rust</a>
<ul>
<li>
<a href="#strace-bpftrace">Strace, bpftrace</a>
</li>
</ul>
</li>
<li>
<a href="#cross-compile">Cross-compile</a>
</li>
<li>
<a href="#conclusion">Conclusion</a>
</li>
<li>
<a href="#addendum-the-full-code">Addendum: the full code</a>
</li>
</ul>
</details>
<p>Rust? Go? Cgo!</p>
<p>I maintain a Go codebase at work which does most of its work through a Rust library that exposes a C API. So they interact via Cgo, Go's FFI mechanism. And it works!</p>
<p>Also, Cgo has many weird limitations and surprises. Fortunately, over the two years or so I have been working in this project, I have (re-)discovered solutions for most of these issues. Let's go through them, and hopefully the next time you use Cgo, you'll have a smooth experience.</p>
<p><em>From Go's perspective, Rust is invisible, the C library looks like a pure C library (and indeed it used to be 100% C++ before it got incrementally rewritten to Rust). So I will use C snippets in this article, because that's what the public C header of the library looks like, and not everybody knows Rust, but most people know a C-like language. But worry not, I will still talk at lengths about Rust in the last section.</em></p>
<p>Let's create a sample app:</p>
<pre>
<div class="code-header">
<span>Plaintext</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-plaintext"><span class="line-number"></span><span class="code-hl">.</span>
<span class="line-number"></span><span class="code-hl">├── app</span>
<span class="line-number"></span><span class="code-hl">│ └── app.go</span>
<span class="line-number"></span><span class="code-hl">├── c</span>
<span class="line-number"></span><span class="code-hl">│ ├── api.c</span>
<span class="line-number"></span><span class="code-hl">│ ├── api.h</span>
<span class="line-number"></span><span class="code-hl">│ ├── api.o</span>
<span class="line-number"></span><span class="code-hl">│ ├── libapi.a</span>
<span class="line-number"></span><span class="code-hl">│ └── Makefile</span>
<span class="line-number"></span><span class="code-hl">├── go.mod</span>
<span class="line-number"></span><span class="code-hl">└── main.go</span>
</code></pre>
<p>The C code is in the <code>c</code> directory, we build a static library <code>libapi.a</code> from it. The public header file is <code>api.h</code>.</p>
<p>The Go code then links this library.</p>
<p><em>The full code can be found at the end of this article.</em></p>
<h2 id="cgo-does-not-have-unions">
<a class="title" href="#cgo-does-not-have-unions">CGO does not have unions</a>
<a class="hash-anchor" href="#cgo-does-not-have-unions" aria-hidden="true" onclick="navigator.clipboard.writeText(this.href);"></a>
</h2>
<p>This is known to Go developers: Go does not have tagged unions, also called sum types, algebraic data types, etc. But C, and Rust, do have them, and Go needs to generate Go types for each C type, so that we can use them! So what does it do? Let's have a look.</p>
<p>So, here is a (very useful) C tagged union:</p>
<pre>
<div class="code-header">
<span>C</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-c"><span class="line-number"></span><span class="code-hl">// c/api.h</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">#pragma once</span>
<span class="line-number"></span><span class="code-hl">#include <stdint.h></span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">typedef struct {</span>
<span class="line-number"></span><span class="code-hl"> char *data;</span>
<span class="line-number"></span><span class="code-hl"> uint64_t len;</span>
<span class="line-number"></span><span class="code-hl">} String;</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">typedef enum {</span>
<span class="line-number"></span><span class="code-hl"> ANIMAL_KIND_DOG,</span>
<span class="line-number"></span><span class="code-hl"> ANIMAL_KIND_CAT,</span>
<span class="line-number"></span><span class="code-hl">} AnimalKind;</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">typedef struct {</span>
<span class="line-number"></span><span class="code-hl"> AnimalKind kind;</span>
<span class="line-number"></span><span class="code-hl"> union {</span>
<span class="line-number"></span><span class="code-hl"> String cat_name; // Only for `ANIMAL_KIND_CAT`.</span>
<span class="line-number"></span><span class="code-hl"> uint16_t dog_tail; // Only for `ANIMAL_KIND_DOG`.</span>
<span class="line-number"></span><span class="code-hl"> };</span>
<span class="line-number"></span><span class="code-hl">} Animal;</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">Animal animal_make_dog();</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">Animal animal_make_cat();</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">void animal_print(Animal *animal);</span>
</code></pre>
<p>The C implementation is straightforward:</p>
<pre>
<div class="code-header">
<span>C</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-c"><span class="line-number"></span><span class="code-hl">// c/api.c</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">#include "api.h"</span>
<span class="line-number"></span><span class="code-hl">#include <assert.h></span>
<span class="line-number"></span><span class="code-hl">#include <inttypes.h></span>
<span class="line-number"></span><span class="code-hl">#include <stdio.h></span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">Animal animal_make_dog() {</span>
<span class="line-number"></span><span class="code-hl"> return (Animal){</span>
<span class="line-number"></span><span class="code-hl"> .kind = ANIMAL_KIND_DOG,</span>
<span class="line-number"></span><span class="code-hl"> .dog_tail = 42,</span>
<span class="line-number"></span><span class="code-hl"> };</span>
<span class="line-number"></span><span class="code-hl">}</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">Animal animal_make_cat() {</span>
<span class="line-number"></span><span class="code-hl"> return (Animal){</span>
<span class="line-number"></span><span class="code-hl"> .kind = ANIMAL_KIND_CAT,</span>
<span class="line-number"></span><span class="code-hl"> .cat_name =</span>
<span class="line-number"></span><span class="code-hl"> {</span>
<span class="line-number"></span><span class="code-hl"> .data = "kitty",</span>
<span class="line-number"></span><span class="code-hl"> .len = 5,</span>
<span class="line-number"></span><span class="code-hl"> },</span>
<span class="line-number"></span><span class="code-hl"> };</span>
<span class="line-number"></span><span class="code-hl">}</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">void animal_print(Animal *animal) {</span>
<span class="line-number"></span><span class="code-hl"> switch (animal->kind) {</span>
<span class="line-number"></span><span class="code-hl"> case ANIMAL_KIND_DOG:</span>
<span class="line-number"></span><span class="code-hl"> printf("Dog: %" PRIu16 "\n", animal->dog_tail);</span>
<span class="line-number"></span><span class="code-hl"> break;</span>
<span class="line-number"></span><span class="code-hl"> case ANIMAL_KIND_CAT:</span>
<span class="line-number"></span><span class="code-hl"> printf("Cat: %.*s\n", (int)animal->cat_name.len, animal->cat_name.data);</span>
<span class="line-number"></span><span class="code-hl"> break;</span>
<span class="line-number"></span><span class="code-hl"> default:</span>
<span class="line-number"></span><span class="code-hl"> assert(0 && "unreachable");</span>
<span class="line-number"></span><span class="code-hl"> }</span>
<span class="line-number"></span><span class="code-hl">}</span>
</code></pre>
<p>And here's how we use it in Go:</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">// app/app.go</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">package app</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">// #cgo CFLAGS: -g -O2 -I${SRCDIR}/../c/</span>
<span class="line-number"></span><span class="code-hl">// #cgo LDFLAGS: ${SRCDIR}/../c/libapi.a</span>
<span class="line-number"></span><span class="code-hl">// #include <api.h></span>
<span class="line-number"></span><span class="code-hl">import "C"</span>
<span class="line-number"></span><span class="code-hl">import "fmt"</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">func DoStuff() {</span>
<span class="line-number"></span><span class="code-hl"> dog := C.animal_make_dog()</span>
<span class="line-number"></span><span class="code-hl"> C.animal_print(&dog)</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl"> cat := C.animal_make_cat()</span>
<span class="line-number"></span><span class="code-hl"> C.animal_print(&cat)</span>
<span class="line-number"></span><span class="code-hl">}</span>
</code></pre>
<p>So far, so good. Let's run it (our <code>main.go</code> simply calls <code>app.DoStuff()</code>):</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ go run .</span>
<span class="line-number"></span><span class="code-hl">Dog: 42</span>
<span class="line-number"></span><span class="code-hl">Cat: kitty</span>
</code></pre>
<p>Great!</p>
<p>Now, let's say we want to access the fields of the C tagged union. We can to have some logic based on whether our cat's name is greater than a limit, say 255? What does the Go struct look like for <code>Animal</code>?</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">type _Ctype_struct___0 struct {</span>
<span class="line-number"></span><span class="code-hl"> kind _Ctype_AnimalKind</span>
<span class="line-number"></span><span class="code-hl"> _ [4]byte</span>
<span class="line-number"></span><span class="code-hl"> anon0 [16]byte</span>
<span class="line-number"></span><span class="code-hl">}</span>
</code></pre>
<p>So it's a struct with a <code>kind</code> field, so far so good. Then comes 4 bytes of padding, as expected (the C struct also has them). But then, we only see 16 opaque bytes. The size is correct: the C union is the size of its largest member which is 16 bytes long (<code>String</code>). But then, how do we access <code>String.len</code>?</p>
<p>Here's the very tedious way, by hand:</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">// app/app.go</span>
<span class="line-number"></span><span class="code-hl">func DoStuff() {</span>
<span class="line-number"></span><span class="code-hl"> // [...]</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl"> cat_ptr := unsafe.Pointer(&cat)</span>
<span class="line-number"></span><span class="code-hl"> cat_name_ptr := unsafe.Add(cat_ptr, 8)</span>
<span class="line-number"></span><span class="code-hl"> cat_name_len_ptr := unsafe.Add(cat_name_ptr, 8)</span>
<span class="line-number"></span><span class="code-hl"> fmt.Println(*(*C.uint64_t)(cat_name_len_ptr))</span>
<span class="line-number"></span><span class="code-hl">}</span>
</code></pre>
<p>And we get:</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ go run .</span>
<span class="line-number"></span><span class="code-hl">Dog: 42</span>
<span class="line-number"></span><span class="code-hl">Cat: kitty</span>
<span class="line-number"></span><span class="code-hl">5</span>
</code></pre>
<p>Ok, we are a C compiler now. Back to computing fields offsets by hand! I sure hope you do not forget about alignment! And keep the offsets in sync with the C struct when its layout changes!</p>
<p>Well we all agree this sucks, but that's all what the <code>unsafe</code> package offers. Cherry on the cake, every pointer in this code has the same type: <code>unsafe.Pointer</code>, even though the first one really is a <code>Animal*</code>, the second one is a <code>String*</code>, and the third one is a <code>uint64_t*</code>. Not great.</p>
<p>So the solution is: treat C unions as opaque values in Go, and only access them with C functions (essentially, getters and setters):</p>
<pre>
<div class="code-header">
<span>C</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-c"><span class="line-number"></span><span class="code-hl">// c/api.h</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">uint16_t animal_dog_get_tail(Animal *animal);</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">String animal_cat_get_name(Animal *animal);</span>
</code></pre>
<pre>
<div class="code-header">
<span>C</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-c"><span class="line-number"></span><span class="code-hl">// c/api.c</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">uint16_t animal_dog_get_tail(Animal *animal) {</span>
<span class="line-number"></span><span class="code-hl"> assert(ANIMAL_KIND_DOG == animal->kind);</span>
<span class="line-number"></span><span class="code-hl"> return animal->dog_tail;</span>
<span class="line-number"></span><span class="code-hl">}</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">String animal_cat_get_name(Animal *animal) {</span>
<span class="line-number"></span><span class="code-hl"> assert(ANIMAL_KIND_CAT == animal->kind);</span>
<span class="line-number"></span><span class="code-hl"> return animal->cat_name;</span>
<span class="line-number"></span><span class="code-hl">}</span>
</code></pre>
<p>And now we have a sane Go code:</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">// app/app.go</span>
<span class="line-number"></span><span class="code-hl">func DoStuff() {</span>
<span class="line-number"></span><span class="code-hl"> // [...]</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl"> cat_name := C.animal_cat_get_name(&cat)</span>
<span class="line-number"></span><span class="code-hl"> fmt.Println(cat_name.len)</span>
<span class="line-number"></span><span class="code-hl">}</span>
</code></pre>
<p>And as a bonus, whenever the layout of <code>Animal</code> changes, for example the order of fields gets changed, or a new field gets added which changes the alignment and thus the padding (here it's not the case because the alignment is already 8 which is the maximum, but in other cases it could happen), the C code gets recompiled, it does the right thing automatically, and everything works as expected.</p>
<p><strong>My recommendation:</strong> never role-play as a compiler, just use getters and setters for unions and let the C compiler do the dirty work.</p>
<p><strong>My ask to the Go team:</strong> mention the approach with getters and setters in the docs. The only thing the <a href="https://pkg.go.dev/cmd/cgo">docs</a> have to say about unions right now is: <code>As Go doesn't have support for C's union type in the general case, C's union types are represented as a Go byte array with the same length</code>. And I don't expect Go to have (tagged) unions anytime soon, so that's the best we can do.</p>
<h2 id="slices-vs-strings">
<a class="title" href="#slices-vs-strings">Slices vs Strings</a>
<a class="hash-anchor" href="#slices-vs-strings" aria-hidden="true" onclick="navigator.clipboard.writeText(this.href);"></a>
</h2>
<p>Quick Go trivia question: what's the difference between <code>[]byte</code> (a slice of bytes) and <code>string</code> (which is a slice of bytes underneath)?</p>
<p>...</p>
<p>The former is mutable while the latter is immutable.
Yes, I might have learned that while writing this article.</p>
<p>Anyways, converting C slices (pointer + length) to Go is straightforward using the <code>unsafe</code> package in modern Go (it used to be much hairier in older Go versions):</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">// app/app.go</span>
<span class="line-number"></span><span class="code-hl">func DoStuff() {</span>
<span class="line-number"></span><span class="code-hl"> // [...]</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl"> cat_name := C.animal_cat_get_name(&cat)</span>
<span class="line-number"></span><span class="code-hl"> slice := unsafe.Slice(cat_name.data, cat_name.len)</span>
<span class="line-number"></span><span class="code-hl"> str := unsafe.String((*byte)(unsafe.Pointer(cat_name.data)), cat_name.len)</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl"> fmt.Println(slice)</span>
<span class="line-number"></span><span class="code-hl"> fmt.Println(str)</span>
<span class="line-number"></span><span class="code-hl">}</span>
</code></pre>
<p>And it does what we expect:</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ go run .</span>
<span class="line-number"></span><span class="code-hl">Dog: 42</span>
<span class="line-number"></span><span class="code-hl">Cat: kitty</span>
<span class="line-number"></span><span class="code-hl">[107 105 116 116 121]</span>
<span class="line-number"></span><span class="code-hl">kitty</span>
</code></pre>
<p>Ok, but just reading them is boring, let's try to mutate them. First, we need to allocate a fresh string in C, otherwise the string constant will be located in the read-only part of the executable, mapped to read-only page, and we will segfault when trying to mutate it. So we modify <code>animal_make_cat</code>:</p>
<pre>
<div class="code-header">
<span>C</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-c"><span class="line-number"></span><span class="code-hl">// c/api.c</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">Animal animal_make_cat() {</span>
<span class="line-number"></span><span class="code-hl"> return (Animal){</span>
<span class="line-number"></span><span class="code-hl"> .kind = ANIMAL_KIND_CAT,</span>
<span class="line-number"></span><span class="code-hl"> .cat_name =</span>
<span class="line-number"></span><span class="code-hl"> {</span>
<span class="line-number"></span><span class="code-hl"> .data = strdup("kitty"), // <= Heap allocation here.</span>
<span class="line-number"></span><span class="code-hl"> .len = 5,</span>
<span class="line-number"></span><span class="code-hl"> },</span>
<span class="line-number"></span><span class="code-hl"> };</span>
<span class="line-number"></span><span class="code-hl">}</span>
</code></pre>
<p>Let's mutate all the things!</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">// app/app.go</span>
<span class="line-number"></span><span class="code-hl">func DoStuff() {</span>
<span class="line-number"></span><span class="code-hl"> // [...]</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl"> slice[0] -= 32 // Poor man's uppercase.</span>
<span class="line-number"></span><span class="code-hl"> fmt.Println(slice)</span>
<span class="line-number"></span><span class="code-hl"> fmt.Println(str)</span>
<span class="line-number"></span><span class="code-hl">}</span>
</code></pre>
<p>And we get the additional output:</p>
<pre>
<div class="code-header">
<span>Plaintext</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-plaintext"><span class="line-number"></span><span class="code-hl">[75 105 116 116 121]</span>
<span class="line-number"></span><span class="code-hl">Kitty</span>
</code></pre>
<p>But wait, this is undefined behavior! The string <em>did</em> get mutated! The Go compiler generates code based on the assumption that strings are immutable, so our program <em>may</em> break in very unexpected ways.</p>
<p>The docs for <code>unsafe.String</code> state:</p>
<blockquote>
<p>Since Go strings are immutable, the bytes passed to String
must not be modified as long as the returned string value exists.</p>
</blockquote>
<p>Maybe the runtime Cgo checks will detect it?</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ GODEBUG=cgocheck=1 go run .</span>
<span class="line-number"></span><span class="code-hl">[...]</span>
<span class="line-number"></span><span class="code-hl">[75 105 116 116 121]</span>
<span class="line-number"></span><span class="code-hl">Kitty</span>
<span class="line-number"></span><span class="code-hl"># Works fine!</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">$ GOEXPERIMENT=cgocheck2 go run . </span>
<span class="line-number"></span><span class="code-hl">[...]</span>
<span class="line-number"></span><span class="code-hl">[75 105 116 116 121]</span>
<span class="line-number"></span><span class="code-hl">Kitty</span>
<span class="line-number"></span><span class="code-hl"># Works fine!</span>
</code></pre>
<p>Nope... so what can we do about it? In my real-life program I have almost no strings to deal with, but some programs will.</p>
<p><strong>My recommendation:</strong></p>
<ul>
<li>In Go, do not use <code>unsafe.String</code>, just use <code>unsafe.Slice</code> and accept that it's mutable everywhere in the program</li>
<li><p>If you really want to use <code>unsafe.String</code>, make sure that the string data returned by the C code is immutable, <strong>enforced by the OS</strong>, so either:</p>
<ul>
<li>It's a constant string placed in the read-only segment</li>
<li>The string data is allocated in its own virtual memory page and the page permissions are changed to read-only before returning the pointer to Go</li>
</ul>
</li>
<li>In C, do not expose string data directly to Go, only expose opaque values (<code>void*</code>), and mutations are only done by calling a C function. That way, the Go caller simply cannot use <code>unsafe.String</code> (I guess they could with lots of casts, but that's not in the realm of a <em>honest mistake</em> anymore).</li>
</ul>
<p><strong>My ask to the Go team:</strong> attempt to develop more advanced checks to detect this issue at runtime.</p>
<h2 id="test-a-c-function-in-go-tests">
<a class="title" href="#test-a-c-function-in-go-tests">Test a C function in Go tests</a>
<a class="hash-anchor" href="#test-a-c-function-in-go-tests" aria-hidden="true" onclick="navigator.clipboard.writeText(this.href);"></a>
</h2>
<p>We are principled programmers who write tests. Let's write a test to ensure that <code>animal_make_dog()</code> does indeed create a dog, i.e. the kind is <code>ANIMAL_KIND_DOG</code>:</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">// app/app_test.go</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">package app</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">import "testing"</span>
<span class="line-number"></span><span class="code-hl">import "C"</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">func TestAnimalMakeDog(t *testing.T) {</span>
<span class="line-number"></span><span class="code-hl"> dog := C.animal_make_dog()</span>
<span class="line-number"></span><span class="code-hl"> _ = dog</span>
<span class="line-number"></span><span class="code-hl">}</span>
</code></pre>
<p>Let's run it:</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ go test ./app/</span>
<span class="line-number"></span><span class="code-hl">use of cgo in test app_test.go not supported</span>
</code></pre>
<p>Ah... yeah this is a <a href="https://go.dev/wiki/cgo">known limitation</a>: <code>_test.go files can't use cgo.</code>.</p>
<p>Solution: wrap the C function in a Go one.</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">// app/app.go</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">func AnimalDogKind() int {</span>
<span class="line-number"></span><span class="code-hl"> return C.ANIMAL_KIND_DOG</span>
<span class="line-number"></span><span class="code-hl">}</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">func AnimalMakeDog() C.Animal {</span>
<span class="line-number"></span><span class="code-hl"> return C.animal_make_dog()</span>
<span class="line-number"></span><span class="code-hl">}</span>
</code></pre>
<p>And we now have a passing Go test:</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">// app/app_test.go</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">package app</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">import "testing"</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">func TestAnimalMakeDog(t *testing.T) {</span>
<span class="line-number"></span><span class="code-hl"> dog := AnimalMakeDog()</span>
<span class="line-number"></span><span class="code-hl"> if int(dog.kind) != AnimalDogKind() {</span>
<span class="line-number"></span><span class="code-hl"> panic("wrong kind")</span>
<span class="line-number"></span><span class="code-hl"> }</span>
<span class="line-number"></span><span class="code-hl">}</span>
</code></pre>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ go test ./app/ -count=1 -v</span>
<span class="line-number"></span><span class="code-hl">=== RUN TestAnimalMakeDog</span>
<span class="line-number"></span><span class="code-hl">--- PASS: TestAnimalMakeDog (0.00s)</span>
<span class="line-number"></span><span class="code-hl">PASS</span>
<span class="line-number"></span><span class="code-hl">ok cgo/app 0.003s</span>
</code></pre>
<p>So... that works, and also: that's annoying boilerplate that no one wants to have to write. And if you're feeling smug, thinking your favorite LLM will do the right thing for you, I can tell you I tried and the LLM generated the wrong thing, with the test trying to use Cgo directly.</p>
<p><strong>My recommendation:</strong></p>
<ul>
<li>Test the C code in C directly (or Rust, or whatever language it is)</li>
<li>There is some glue code sometimes that is only useful to the Go codebase, and that's written in C. In that case wrap each C utility function in a Go one and write a Go test, hopefully it's not that much.</li>
</ul>
<p><strong>My ask to the Go team:</strong> let's allow the use of Cgo in tests.</p>
<h2 id="the-go-compiler-does-not-detect-changes">
<a class="title" href="#the-go-compiler-does-not-detect-changes">The Go compiler does not detect changes</a>
<a class="hash-anchor" href="#the-go-compiler-does-not-detect-changes" aria-hidden="true" onclick="navigator.clipboard.writeText(this.href);"></a>
</h2>
<p>People are used to say: Go builds so fast! And yes, it's not a slow compiler, but if you have ever built the Go compiler from scratch, you will have noticed it takes a significant amount of time still. What Go is really good at, is caching: it's really smart at detecting what changed, and only rebuilding that. And that's great! Until it isn't.</p>
<p>Sometimes, changes to the Cgo build flags, or to the <code>.a</code> library, were not detected by Go. I could not really reproduce these issues reliably, but they do happen.</p>
<p>Solution: force a clean build with <code>go build -a</code>.</p>
<h2 id="false-positive-warnings">
<a class="title" href="#false-positive-warnings">False positive warnings</a>
<a class="hash-anchor" href="#false-positive-warnings" aria-hidden="true" onclick="navigator.clipboard.writeText(this.href);"></a>
</h2>
<p>Sometimes we need to run some C code once at startup, when the package gets initialized:</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">// app/app.go</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">package app</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">// #cgo CFLAGS: -g -O2 -I${SRCDIR}/../c/</span>
<span class="line-number"></span><span class="code-hl">// #cgo LDFLAGS: ${SRCDIR}/../c/libapi.a</span>
<span class="line-number"></span><span class="code-hl">// #include <api.h></span>
<span class="line-number"></span><span class="code-hl">// void initial_setup();</span>
<span class="line-number"></span><span class="code-hl">import "C"</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">func init() {</span>
<span class="line-number"></span><span class="code-hl"> C.initial_setup()</span>
<span class="line-number"></span><span class="code-hl">}</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">[...]</span>
</code></pre>
<p>And the C function <code>initial_setup</code> is defined in a second file in the same Go package (this is not strictly necessary but it will turn out to be useful to showcase something later):</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">// app/cfuncs.go</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">package app</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">/*</span>
<span class="line-number"></span><span class="code-hl">void initial_setup(){</span>
<span class="line-number"></span><span class="code-hl"> // Do some useful stuff.</span>
<span class="line-number"></span><span class="code-hl">}</span>
<span class="line-number"></span><span class="code-hl">*/</span>
<span class="line-number"></span><span class="code-hl">import "C"</span>
</code></pre>
<p>Yes, we can write C code directly in Go files. Inside comments. Not, it's not weird at all.</p>
<p>We build, everything is fine:</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ go build .</span>
</code></pre>
<p>Since we are serious programmers, we want to enable C warnings, right? Let's add <code>-Wall</code> to the <code>CFLAGS</code>:</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">// app/app.go</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">[...]</span>
<span class="line-number"></span><span class="code-hl">// #cgo CFLAGS: -Wall -g -O2 -I${SRCDIR}/../c/ <= We add -Wall</span>
<span class="line-number"></span><span class="code-hl">[...]</span>
</code></pre>
<p>We re-build, and get this nice error:</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">$ go build .</span>
<span class="line-number"></span><span class="code-hl"># cgo/app</span>
<span class="line-number"></span><span class="code-hl">cgo-gcc-prolog: In function ‘_cgo_13d20cc583d0_Cfunc_initial_setup’:</span>
<span class="line-number"></span><span class="code-hl">cgo-gcc-prolog:78:49: warning: unused variable ‘_cgo_a’ [-Wunused-variable]</span>
</code></pre>
<p>Wait, we do not have <em>any</em> variable in <code>initial_setup</code>, how come a variable is unused?</p>
<p>Some searching around turns up this <a href="https://github.com/golang/go/issues/6883#issuecomment-383800123,">Github issue</a> where the official recommendation is: do not use <code>-Wall</code>, it creates false positives. Ok.</p>
<p><strong>My recommendation:</strong> Write C code in C files and enable all the warnings you want.</p>
<p><strong>My ask to the Go team:</strong> Let's please fix the false positives and allow people to enable some basic warnings. <code>-Wall</code> is the bare minimum!</p>
<h2 id="white-space-is-significant">
<a class="title" href="#white-space-is-significant">White space is significant</a>
<a class="hash-anchor" href="#white-space-is-significant" aria-hidden="true" onclick="navigator.clipboard.writeText(this.href);"></a>
</h2>
<p>Let's go back to the <code>app/cfuncs.go</code> we just created that builds fine:</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">package app</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">/*</span>
<span class="line-number"></span><span class="code-hl">void initial_setup(){}</span>
<span class="line-number"></span><span class="code-hl">*/</span>
<span class="line-number"></span><span class="code-hl">import "C"</span>
</code></pre>
<p>Let's add one empty line near the end:</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">package app</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">/*</span>
<span class="line-number"></span><span class="code-hl">void initial_setup(){}</span>
<span class="line-number"></span><span class="code-hl">*/</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">import "C"</span>
</code></pre>
<p>Let's build:</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ go build .</span>
<span class="line-number"></span><span class="code-hl"># cgo</span>
<span class="line-number"></span><span class="code-hl">/home/pg/Downloads/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1</span>
<span class="line-number"></span><span class="code-hl">/usr/bin/gcc -m64 -o $WORK/b001/exe/a.out -Wl,--export-dynamic-symbol=_cgo_panic -Wl,--export-dynamic-symbol=_cgo_topofstack -Wl,--export-dynamic-symbol=crosscall2 -Wl,--compress-debug-sections=zlib /tmp/go-link-2748897775/go.o /tmp/go-link-2748897775/000000.o /tmp/go-link-2748897775/000001.o /tmp/go-link-2748897775/000002.o /tmp/go-link-2748897775/000003.o /tmp/go-link-2748897775/000004.o /tmp/go-link-2748897775/000005.o /tmp/go-link-2748897775/000006.o /tmp/go-link-2748897775/000007.o /tmp/go-link-2748897775/000008.o /tmp/go-link-2748897775/000009.o /tmp/go-link-2748897775/000010.o /tmp/go-link-2748897775/000011.o /tmp/go-link-2748897775/000012.o /tmp/go-link-2748897775/000013.o /tmp/go-link-2748897775/000014.o /tmp/go-link-2748897775/000015.o /tmp/go-link-2748897775/000016.o -O2 -g /home/pg/scratch/cgo/app/../c/libapi.a -O2 -g -lpthread -no-pie</span>
<span class="line-number"></span><span class="code-hl">/usr/bin/ld: /tmp/go-link-2748897775/000001.o: in function `_cgo_f1a74d84225f_Cfunc_initial_setup':</span>
<span class="line-number"></span><span class="code-hl">/tmp/go-build/cgo-gcc-prolog:80:(.text+0x53): undefined reference to `initial_setup'</span>
<span class="line-number"></span><span class="code-hl">collect2: error: ld returned 1 exit status</span>
</code></pre>
<p>Ok... not much to say here.</p>
<p>Here's another example. We add a comment about not using <code>-Wall</code>:</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">// app/app.go</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">package app</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">// NOTE: Do not use -Wall.</span>
<span class="line-number"></span><span class="code-hl">// #cgo CFLAGS: -g -O2 -I${SRCDIR}/../c/</span>
<span class="line-number"></span><span class="code-hl">// #cgo LDFLAGS: ${SRCDIR}/../c/libapi.a</span>
<span class="line-number"></span><span class="code-hl">// #include <api.h></span>
<span class="line-number"></span><span class="code-hl">// void initial_setup();</span>
<span class="line-number"></span><span class="code-hl">import "C"</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">[...]</span>
</code></pre>
<p>We rebuild, and boom:</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ go build .</span>
<span class="line-number"></span><span class="code-hl"># cgo/app</span>
<span class="line-number"></span><span class="code-hl">app/app.go:3:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token</span>
<span class="line-number"></span><span class="code-hl"> 3 | // NOTE: Do not use -Wall.</span>
<span class="line-number"></span><span class="code-hl"> | ^</span>
</code></pre>
<p>That's because when seeing a <code>#cgo</code> directive in the comments, the Go compiler parses what it recognizes, passes the rest to the C compiler, which chokes on it.</p>
<p>Solution: insert a blank line between the comment and the <code>#cgo</code> directive to avoid that:</p>
<pre>
<div class="code-header">
<span>Go</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-go"><span class="line-number"></span><span class="code-hl">// app/app.go</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">package app</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">// NOTE: Do not use -Wall.</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">// #cgo CFLAGS: -g -O2 -I${SRCDIR}/../c/</span>
<span class="line-number"></span><span class="code-hl">// #cgo LDFLAGS: ${SRCDIR}/../c/libapi.a</span>
<span class="line-number"></span><span class="code-hl">// #include <api.h></span>
<span class="line-number"></span><span class="code-hl">// void initial_setup();</span>
<span class="line-number"></span><span class="code-hl">import "C"</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">[...]</span>
</code></pre>
<p><strong>My recommendation:</strong> If you get a hairy and weird error, compare the white space with official code examples.</p>
<p><strong>My ask to the Go team:</strong> Can we please fix this? Or at least document it? There is zero mention of this pitfall anywhere, as far as I can see.</p>
<h2 id="debug-go-and-c-rust">
<a class="title" href="#debug-go-and-c-rust">Debug Go and C/Rust</a>
<a class="hash-anchor" href="#debug-go-and-c-rust" aria-hidden="true" onclick="navigator.clipboard.writeText(this.href);"></a>
</h2>
<p>It's very simple, you have two exclusive choices:</p>
<ul>
<li>Use a C/Rust debugger (e.g gdb, lldb, etc), which does not understand the Go calling convention so you can see the C/Rust call stack, but it ends at the Cgo FFI boundary, or</li>
<li>Use a Go debugger (e.g. delve) which does not (really) understand the C/Rust calling convention so you can see the Go stack, but it ends at the Cgo FFI boundary</li>
</ul>
<p>Let's see it for ourselves:</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ gdb ./cgo</span>
<span class="line-number"></span><span class="code-hl">(gdb) break animal_print</span>
<span class="line-number"></span><span class="code-hl">Breakpoint 1 at 0x469b6c</span>
<span class="line-number"></span><span class="code-hl">(gdb) run</span>
<span class="line-number"></span><span class="code-hl">[...]</span>
<span class="line-number"></span><span class="code-hl">Thread 1 "cgo" hit Breakpoint 1, 0x0000000000469b6c in animal_print ()</span>
<span class="line-number"></span><span class="code-hl">(gdb) backtrace</span>
<span class="line-number"></span><span class="code-hl">#0 0x0000000000469b6c in animal_print ()</span>
<span class="line-number"></span><span class="code-hl">#1 0x0000000000464644 in runtime.asmcgocall () at /home/pg/Downloads/go/src/runtime/asm_amd64.s:923</span>
<span class="line-number"></span><span class="code-hl">#2 0x000000c0000061c0 in ?? ()</span>
<span class="line-number"></span><span class="code-hl">#3 0x0000000000462a0a in runtime.systemstack () at /home/pg/Downloads/go/src/runtime/asm_amd64.s:514</span>
<span class="line-number"></span><span class="code-hl">#4 0x00007fffffffe228 in ?? ()</span>
<span class="line-number"></span><span class="code-hl">#5 0x0000000000466f3f in runtime.newproc (fn=0x46288f <runtime.rt0_go+303>) at <autogenerated>:1</span>
<span class="line-number"></span><span class="code-hl">#6 0x0000000000462905 in runtime.mstart () at /home/pg/Downloads/go/src/runtime/asm_amd64.s:395</span>
<span class="line-number"></span><span class="code-hl">#7 0x000000000046288f in runtime.rt0_go () at /home/pg/Downloads/go/src/runtime/asm_amd64.s:358</span>
<span class="line-number"></span><span class="code-hl">#8 0x0000000000000001 in ?? ()</span>
<span class="line-number"></span><span class="code-hl">#9 0x00007fffffffe388 in ?? ()</span>
<span class="line-number"></span><span class="code-hl">#10 0x00007fffffffe340 in ?? ()</span>
<span class="line-number"></span><span class="code-hl">#11 0x0000000000000001 in ?? ()</span>
<span class="line-number"></span><span class="code-hl">#12 0x00007fffffffe388 in ?? ()</span>
<span class="line-number"></span><span class="code-hl">#13 0x00007ffff7db1248 in __libc_start_call_main (main=0x300000002, argc=192, argv=0x43a3c5 <runtime.reentersyscall+165>) at ../sysdeps/nptl/libc_start_call_main.h:58</span>
<span class="line-number"></span><span class="code-hl">Backtrace stopped: previous frame inner to this frame (corrupt stack?)</span>
</code></pre>
<p>Where is <code>app.DoStuff</code>? Where is <code>main</code>? Probably around frames 8-13 in the <code>corrupt stack</code>...</p>
<p>Now with <code>delve</code>:</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ go build -gcflags=all="-N -l"</span>
<span class="line-number"></span><span class="code-hl">$ dlv exec ./cgo</span>
<span class="line-number"></span><span class="code-hl">(dlv) b animal_print</span>
<span class="line-number"></span><span class="code-hl">Command failed: could not find function C.animal_print</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">(dlv) b app.DoStuff</span>
<span class="line-number"></span><span class="code-hl">Breakpoint 1 set at 0x471f2a for cgo/app.DoStuff() ./app/app.go:23</span>
<span class="line-number"></span><span class="code-hl">(dlv) c</span>
<span class="line-number"></span><span class="code-hl">> [Breakpoint 1] cgo/app.DoStuff() ./app/app.go:23 (hits goroutine(1):1 total:1) (PC: 0x471f2a)</span>
<span class="line-number"></span><span class="code-hl"> 18: </span>
<span class="line-number"></span><span class="code-hl"> 19: func AnimalMakeDog() C.Animal {</span>
<span class="line-number"></span><span class="code-hl"> 20: return C.animal_make_dog()</span>
<span class="line-number"></span><span class="code-hl"> 21: }</span>
<span class="line-number"></span><span class="code-hl"> 22: </span>
<span class="line-number"></span><span class="code-hl">=> 23: func DoStuff() {</span>
<span class="line-number"></span><span class="code-hl"> 24: dog := C.animal_make_dog()</span>
<span class="line-number"></span><span class="code-hl"> 25: C.animal_print(&dog)</span>
<span class="line-number"></span><span class="code-hl"> 26: </span>
<span class="line-number"></span><span class="code-hl"> 27: }</span>
<span class="line-number"></span><span class="code-hl">(dlv) s</span>
<span class="line-number"></span><span class="code-hl">> cgo/app.DoStuff() ./app/app.go:24 (PC: 0x471f2e)</span>
<span class="line-number"></span><span class="code-hl"> 19: func AnimalMakeDog() C.Animal {</span>
<span class="line-number"></span><span class="code-hl"> 20: return C.animal_make_dog()</span>
<span class="line-number"></span><span class="code-hl"> 21: }</span>
<span class="line-number"></span><span class="code-hl"> 22: </span>
<span class="line-number"></span><span class="code-hl"> 23: func DoStuff() {</span>
<span class="line-number"></span><span class="code-hl">=> 24: dog := C.animal_make_dog()</span>
<span class="line-number"></span><span class="code-hl"> 25: C.animal_print(&dog)</span>
<span class="line-number"></span><span class="code-hl"> 26: </span>
<span class="line-number"></span><span class="code-hl"> 27: }</span>
<span class="line-number"></span><span class="code-hl">(dlv) </span>
<span class="line-number"></span><span class="code-hl">> cgo/app._Cfunc_animal_make_dog() _cgo_gotypes.go:79 (PC: 0x471d93)</span>
<span class="line-number"></span><span class="code-hl">(dlv) </span>
<span class="line-number"></span><span class="code-hl">> cgo/app._Cfunc_animal_make_dog() _cgo_gotypes.go:80 (PC: 0x471dab)</span>
<span class="line-number"></span><span class="code-hl">(dlv) </span>
<span class="line-number"></span><span class="code-hl">> cgo/app._Cfunc_animal_make_dog() _cgo_gotypes.go:81 (PC: 0x471dd3)</span>
<span class="line-number"></span><span class="code-hl">(dlv) </span>
<span class="line-number"></span><span class="code-hl">> cgo/app._Cfunc_animal_make_dog() _cgo_gotypes.go:83 (PC: 0x471de4)</span>
<span class="line-number"></span><span class="code-hl">(dlv) </span>
<span class="line-number"></span><span class="code-hl">> cgo/app.DoStuff() ./app/app.go:24 (PC: 0x471f45)</span>
<span class="line-number"></span><span class="code-hl">Values returned:</span>
<span class="line-number"></span><span class="code-hl"> r1: cgo/app._Ctype_struct___0 {</span>
<span class="line-number"></span><span class="code-hl"> kind: 0,</span>
<span class="line-number"></span><span class="code-hl"> _: [4]uint8 [0,0,0,0],</span>
<span class="line-number"></span><span class="code-hl"> anon0: [16]uint8 [42,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],}</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl"> 19: func AnimalMakeDog() C.Animal {</span>
<span class="line-number"></span><span class="code-hl"> 20: return C.animal_make_dog()</span>
<span class="line-number"></span><span class="code-hl"> 21: }</span>
<span class="line-number"></span><span class="code-hl"> 22: </span>
<span class="line-number"></span><span class="code-hl"> 23: func DoStuff() {</span>
<span class="line-number"></span><span class="code-hl">=> 24: dog := C.animal_make_dog()</span>
<span class="line-number"></span><span class="code-hl"> 25: C.animal_print(&dog)</span>
<span class="line-number"></span><span class="code-hl"> 26: </span>
<span class="line-number"></span><span class="code-hl"> 27: }</span>
<span class="line-number"></span><span class="code-hl">(dlv) </span>
<span class="line-number"></span><span class="code-hl">> cgo/app.DoStuff() ./app/app.go:25 (PC: 0x471f67)</span>
<span class="line-number"></span><span class="code-hl"> 20: return C.animal_make_dog()</span>
<span class="line-number"></span><span class="code-hl"> 21: }</span>
<span class="line-number"></span><span class="code-hl"> 22: </span>
<span class="line-number"></span><span class="code-hl"> 23: func DoStuff() {</span>
<span class="line-number"></span><span class="code-hl"> 24: dog := C.animal_make_dog()</span>
<span class="line-number"></span><span class="code-hl">=> 25: C.animal_print(&dog)</span>
<span class="line-number"></span><span class="code-hl"> 26: </span>
<span class="line-number"></span><span class="code-hl"> 27: }</span>
<span class="line-number"></span><span class="code-hl">(dlv) </span>
<span class="line-number"></span><span class="code-hl">> cgo/app.DoStuff() ./app/app.go:25 (PC: 0x471f67)</span>
<span class="line-number"></span><span class="code-hl"> 20: return C.animal_make_dog()</span>
<span class="line-number"></span><span class="code-hl"> 21: }</span>
<span class="line-number"></span><span class="code-hl"> 22: </span>
<span class="line-number"></span><span class="code-hl"> 23: func DoStuff() {</span>
<span class="line-number"></span><span class="code-hl"> 24: dog := C.animal_make_dog()</span>
<span class="line-number"></span><span class="code-hl">=> 25: C.animal_print(&dog)</span>
<span class="line-number"></span><span class="code-hl"> 26: </span>
<span class="line-number"></span><span class="code-hl"> 27: }</span>
<span class="line-number"></span><span class="code-hl">(dlv) s</span>
<span class="line-number"></span><span class="code-hl">> cgo/app._Cfunc_animal_print() _cgo_gotypes.go:91 (PC: 0x471e13)</span>
<span class="line-number"></span><span class="code-hl">(dlv) s</span>
<span class="line-number"></span><span class="code-hl">> cgo/app._Cfunc_animal_print() _cgo_gotypes.go:92 (PC: 0x471e17)</span>
<span class="line-number"></span><span class="code-hl">(dlv) stack</span>
<span class="line-number"></span><span class="code-hl">0 0x0000000000471e17 in cgo/app._Cfunc_animal_print</span>
<span class="line-number"></span><span class="code-hl"> at _cgo_gotypes.go:92</span>
<span class="line-number"></span><span class="code-hl">1 0x0000000000471f75 in cgo/app.DoStuff</span>
<span class="line-number"></span><span class="code-hl"> at ./app/app.go:25</span>
<span class="line-number"></span><span class="code-hl">2 0x000000000047228f in main.main</span>
<span class="line-number"></span><span class="code-hl"> at ./main.go:6</span>
<span class="line-number"></span><span class="code-hl">3 0x0000000000437a07 in runtime.main</span>
<span class="line-number"></span><span class="code-hl"> at /home/pg/Downloads/go/src/runtime/proc.go:272</span>
<span class="line-number"></span><span class="code-hl">4 0x000000000046c301 in runtime.goexit</span>
<span class="line-number"></span><span class="code-hl"> at /home/pg/Downloads/go/src/runtime/asm_amd64.s:1700</span>
</code></pre>
<p>So the Go debugger does not understand C/Rust debugging information, so as soon as we are inside a C/Rust function, it cannot show anything useful, but it can still kind of understand the call stack...Urgh.</p>
<p>And the <a href="https://go.dev/doc/gdb">docs</a> acknowledge that:</p>
<blockquote>
<p>GDB does not understand Go programs well. The stack management, threading, and runtime contain aspects that differ enough from the execution model GDB expects that they can confuse the debugger and cause incorrect results even when the program is compiled with gccgo. As a consequence, although GDB can be useful in some situations (e.g., debugging Cgo code, or debugging the runtime itself), it is not a reliable debugger for Go programs, particularly heavily concurrent ones. Moreover, it is not a priority for the Go project to address these issues, which are difficult.</p>
</blockquote>
<p>And it's not an issue of missing debugging information, I compiled the C code with <code>-g</code>, or <code>-g3</code>.</p>
<p>This point is close to being a deal-breaker for me. Debugging is really important! A language/tech stack is IMHO only as good as we developers can understand and troubleshoot production applications. Can't debug, can't pinpoint where the bug is? Not sure if that program is worth much.</p>
<p><strong>My recommendation:</strong>: Have both debuggers at hand and locate where the problem is: is it on the Go side or on the C/Rust side? Then use the right debugger to inspect local variables and such. Yes, it's a pity. I guess you can try to build Go code with Gccgo, perhaps gdb understands the full call stack then? My approach was to insert logs everywhere in the code, both Go and Rust, with logs. Not ideal. It's a bit too close to 'printf debugging' to my taste.</p>
<p><strong>My ask for the Go team:</strong>: Well, ideally both debuggers would work fully with CGO. But since this issue is known for years...I don't have much hope.</p>
<h3 id="strace-bpftrace">
<a class="title" href="#strace-bpftrace">Strace, bpftrace</a>
<a class="hash-anchor" href="#strace-bpftrace" aria-hidden="true" onclick="navigator.clipboard.writeText(this.href);"></a>
</h3>
<p>It's the same issue manifesting in a different way: it's not just debuggers than do not understand the call stack, it's also <code>strace</code>:</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ strace -k -e write ./cgo</span>
<span class="line-number"></span><span class="code-hl">--- SIGURG {si_signo=SIGURG, si_code=SI_TKILL, si_pid=438876, si_uid=1000} ---</span>
<span class="line-number"></span><span class="code-hl"> > /usr/lib64/libc.so.6(pthread_sigmask@GLIBC_2.2.5+0x48) [0x783b8]</span>
<span class="line-number"></span><span class="code-hl"> > /home/pg/scratch/cgo/cgo(_cgo_sys_thread_start+0x7e) [0x4727ee]</span>
<span class="line-number"></span><span class="code-hl"> > /home/pg/scratch/cgo/cgo(runtime.asmcgocall.abi0+0x9c) [0x46c01c]</span>
<span class="line-number"></span><span class="code-hl">write(1, "Dog: 42\n", 8Dog: 42</span>
<span class="line-number"></span><span class="code-hl">) = 8</span>
<span class="line-number"></span><span class="code-hl"> > /usr/lib64/libc.so.6(__write+0x4d) [0xe853d]</span>
<span class="line-number"></span><span class="code-hl"> > /usr/lib64/libc.so.6(_IO_file_write@@GLIBC_2.2.5+0x34) [0x68fa4]</span>
<span class="line-number"></span><span class="code-hl"> > /usr/lib64/libc.so.6(new_do_write+0x5c) [0x6711c]</span>
<span class="line-number"></span><span class="code-hl"> > /usr/lib64/libc.so.6(_IO_do_write@@GLIBC_2.2.5+0x20) [0x67fb0]</span>
<span class="line-number"></span><span class="code-hl"> > /usr/lib64/libc.so.6(_IO_file_overflow@@GLIBC_2.2.5+0x11a) [0x6852a]</span>
<span class="line-number"></span><span class="code-hl"> > /usr/lib64/libc.so.6(_IO_default_xsputn+0x74) [0x6a624]</span>
<span class="line-number"></span><span class="code-hl"> > /usr/lib64/libc.so.6(_IO_file_xsputn@@GLIBC_2.2.5+0x117) [0x69127]</span>
<span class="line-number"></span><span class="code-hl"> > /usr/lib64/libc.so.6(__printf_buffer_flush_to_file+0xc8) [0x36448]</span>
<span class="line-number"></span><span class="code-hl"> > /usr/lib64/libc.so.6(__printf_buffer_to_file_done+0x1b) [0x3650b]</span>
<span class="line-number"></span><span class="code-hl"> > /usr/lib64/libc.so.6(__vfprintf_internal+0xaa) [0x41bea]</span>
<span class="line-number"></span><span class="code-hl"> > /usr/lib64/libc.so.6(printf+0xb2) [0x35bf2]</span>
<span class="line-number"></span><span class="code-hl"> > /home/pg/scratch/cgo/cgo(animal_print+0x38) [0x472da0]</span>
<span class="line-number"></span><span class="code-hl"> > /home/pg/scratch/cgo/cgo(runtime.asmcgocall.abi0+0x63) [0x46bfe3]</span>
<span class="line-number"></span><span class="code-hl"> > No DWARF information found</span>
<span class="line-number"></span><span class="code-hl">+++ exited with 0 +++</span>
</code></pre>
<p>Same as gdb, the call stack stops at the Cgo FFI boundary.</p>
<p>But surprisingly, <code>bpftrace</code> seems to work:</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ sudo bpftrace -e 'uprobe:./cgo:animal_print {print(ustack(perf, 64))}' -c ./cgo</span>
<span class="line-number"></span><span class="code-hl">Attaching 1 probe...</span>
<span class="line-number"></span><span class="code-hl">Dog: 42</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl"> 472d68 animal_print+0 (./cgo)</span>
<span class="line-number"></span><span class="code-hl"> 46bfe4 runtime.asmcgocall.abi0+100 (./cgo)</span>
<span class="line-number"></span><span class="code-hl"> 46361f runtime.cgocall+127 (./cgo)</span>
<span class="line-number"></span><span class="code-hl"> 471e3f cgo/app._Cfunc_animal_print.abi0+63 (./cgo)</span>
<span class="line-number"></span><span class="code-hl"> 471f75 cgo/app.DoStuff+85 (./cgo)</span>
<span class="line-number"></span><span class="code-hl"> 47228f main.main+15 (./cgo)</span>
<span class="line-number"></span><span class="code-hl"> 437a07 runtime.main+583 (./cgo)</span>
<span class="line-number"></span><span class="code-hl"> 46c301 runtime.goexit.abi0+1 (./cgo)</span>
</code></pre>
<p>So, let's use <code>bpftrace</code>, I guess.</p>
<h2 id="cross-compile">
<a class="title" href="#cross-compile">Cross-compile</a>
<a class="hash-anchor" href="#cross-compile" aria-hidden="true" onclick="navigator.clipboard.writeText(this.href);"></a>
</h2>
<p>So picture me, building my Go program (a web service) using Cgo. Locally, it builds very quickly, due to Rust and Go caching.</p>
<p>Now, time to build in Docker to be able to test my changes:</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ time docker build [...]</span>
<span class="line-number"></span><span class="code-hl">[...]</span>
<span class="line-number"></span><span class="code-hl">Total execution time 101.664413146</span>
</code></pre>
<p>That's in seconds. Every. Single. Time. Urgh.</p>
<p>The issue is that little to no caching is being leveraged by either compiler. Dependencies are fetched from the internet, and essentially, a clean release build is performed. That takes a looong time. Past developers tried to fix this by volume mounting host directories inside Docker but apparently, they missed a few.</p>
<p>That's why I am convinced that Docker is not meant to build stuff. Only to run stuff. I have seen the same problem with C++ codebases being built in Docker, with Rust codebases being built in Docker, etc. Even with JavaScript/Typescript codebases built in docker (these were for some reason often the slowest to build). In the worst cases it would take over an hour to build, and developers resorted to duplicate the deployment setup to be able to run (and thus test) the app locally, completely bypassing Docker.</p>
<p>I think the original intent to do a clean build inside Docker was because developers feared that the local environment was somehow tainted and/or that the compiler would mess up with the caching and result in a borked build. And that was the case I believe, with most C/C++ codebase relying on globally installed libraries built how-knows-how. But modern compilers are, from my perspective, really good at identifying changes, correctly caching what did not change, and rebuilding with the correct build flags, what does need to be rebuilt. And developers have improved on the Bill Of Material side. Much work has been done on existing tools to get reproducible builds. New tools have emerged, like Nix, that focus on controlled reproducible builds.</p>
<p>So in my opinion, the ideal Docker build process is: a single static executable is built locally, relying on caching of previous builds (or possibly in CI, remote intermediate artifacts). Then, it is copied inside the image which, again ideally, for security purposes, is very bare bone. The dockerfile can look like this:</p>
<pre>
<div class="code-header">
<span>Dockerfile</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-dockerfile"><span class="line-number"></span><span class="code-hl">FROM gcr.io/distroless/static:nonroot</span>
<span class="line-number"></span><span class="code-hl">USER nonroot</span>
<span class="line-number"></span><span class="code-hl">WORKDIR /home/nonroot</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">COPY --chown=nonroot:nonroot app.exe .</span>
<span class="line-number"></span><span class="code-hl"></span>
<span class="line-number"></span><span class="code-hl">CMD ["/home/nonroot/app.exe"]</span>
</code></pre>
<p>It's fast, simple, secure. But, to make it work, regardless of the host, we need to cross-compile.</p>
<p>Go is praised for its uncomplicated cross-compiling support. But this goes out of the window when Cgo is enabled. Let's try:</p>
<pre>
<div class="code-header">
<span>Shell</span>
<button class="copy-code" type="button"><svg aria-hidden="true" focusable="false" class="octicon octicon-copy" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align: text-bottom;"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg></button>
</div>
<code class="language-shell"><span class="line-number"></span><span class="code-hl">$ GOOS=linux GOARCH=arm go build .</span>
<span class="line-number"></span><span class="code-hl">cgo/app: build constraints exclude all Go files in /home/pg/scratch/cgo/app</span>
</code></pre>
<p>It fails. But fortunately, Go still supports cross-compiling with Cgo as long as we provide it with a cross-compiler.</p>
<p>After some experimentation, my favorite way is to use <a href="https://dev.to/kristoff/zig-makes-go-cross-compilation-just-work-29ho">Zig</a> for that. That way it works the same way for people using macOS, Linux, be it on ARM, on x86_64, etc. And it makes it trivial to build native Docker images for ARM without changing the whole build system or installing additional tools.</p>
<p>The work on Zig is fantastic, please consider supporting them!</p>
<p>So, how does it look like? Let's assume we want to target <code>x86_64-linux-musl</code>, built statically, since we use a distroless image that does not come with a libc. The benefit is that our service looks like any other Go service without Cgo.</p>
<p>We could also target a specific glibc version and deploy on a LTS version of Ubuntu, Debian, etc. Zig supports that.</p>
<p>First, we cross-compile our C code:</p>