8                             
                                                                                                                                                                                                                                                                              888                           
                   S SSXSSSXX%S@X%X@X%X@          8 X X@S@X@X@X@X@@@@@@@@@@@@@8             XXX@X@X@X@@@@@@@@@@@@8@8@8@8            888888888888888888888888888888                                                     88888888888888888888                   888888888888    88@8                          
                    %SX               X S         @S%@                       8XS            8@X@                       @@@            888                       888                                                    8888               888                 88       88X    888888                        
                       X%               SSS       XSX  @                       8S@          8X  8X                      @@@@            8@8                       888                                                  88  88               888               88       888    88  88tX                      
                         S                % S     S%X   X                        8%X        8X    8                       XX@            @@8@                      8888                                                88    88              888              88       888    88    8888                    
                           S                SSX   %X@     S                       8S@@      8      8S                       8@@            @X@                       888                8                              88      8               888            88       888    88      888                   
                            X S S S S S S XS 8X    @S      % SSX8 SSXSSSXSSSXX%S@X88X@@     8S       8%XX@X@X@X@X@@@@@@@@@@@XXS@             X@X@X@X@@@@@@@@@@@@8@8@88@888            88888                            88       88              888           8        8@8    88        888                 
                             X                @    S                %S                      @        8               8       8X%             8@X               @X                    8888@8@8                          88         88             88           88       888    8          8888               
                            .S  .S            S    XS                S                      XS       @               8       8SS             8X8               XX                     @8X@@8X88                        88           8            88           88       888    88           888              
                            :S    :S          S    XX                S                      S        X               @       8 X             8X                8@                       @@@X8X8X@                      88            88          88           88       888    88             888            
                            ;S      :        .S   .S                 S                       X       %               X       @S              8S8               8X                         @@SXX8S@@                    88              88       888           88       888888888              8888          
                            tS       ;S      ;S   :X        .        S                       S                       S       X%@             8SS               8S                           X@XS8S@@@                  8@8               8888888888           88       88      88              88@          
                            %X         tS    tS   ;X%       :       .S S S S                 S                       %       S%X             @SS               8S                             8XS8S8@@X                  @8@               888                88       8       88 8             88          
                            SX           %   %X   tS        ;       :X X X X S               S                               %X@             X @               8                                8X8S8%XX@                 @@@@              8888              88       8       8   88           88          
                            XX             S SX   %8.       t       ;        :X S            S                                XX             SSS               @S                                  8SXSXSXS            XX   @X@               @88             88       8       8     88         88          
                            @X              X8;   SS%       %       t          ;S.          :S                                S               XS               X                                     8S8X8SXS          8@XX   8@@               @@@           8@       8       8       8        88          
                            8@               X8   X8t       S       %            tS;        ;S       :                        X%              SSX              SS                                      8@@X88 X        8X8 @   8X@S              @@@@         @8       8       8        88      88          
                            8X               @S   @XS       X       S             %@;S      tS       ;               .        XS              XS  S             S                                       8S8SSSSX8      8X  8X@   8XS               8@@        @X       @       8          88    88          
                            8X               8@   888       @       XXXXXXSXSXSXS@St8:%     %X       t               :       .S               SS                S                                     @ XSS8 SX        8@    8S@   8S@               8@@      XX       @       @            88  88          
          8@8X8X8@8X8X8@8X8@8@               8X   8XS       8       @8                      SX       %               ;       :X               XS     S          S                                   X XSX88S           8X     8S@8  8X@               88@8    8@       X       @              @@88          
           8XSXS@S@SXXXX@S8888               8X   888       8       8X                      XX       S               t       ;X%             .S        S        S                                 % @X 88XS            @S       8 X XX@8 X X@S@X@X@X@@8@8@%   8X       8@@888@XXX              @@@          
            8888             8               8@   888       8       8X                      @X       X               %       tS              :X         .       S                                SS@ %88X              XX         @                      8S   8S       8@@X@XS8 8%              @@          
              8X8            8               88   888       8       8X                      @X       @               S       %8.             tX%          :    .S                              X% S8888                %           X                     8    8S       8SS       8X@            8@          
                8@8          8               8@   888       8       8@                      8@       @               X       SS%             %X            ;S  ;S                            X S 888                    X            S                   @S   8        8S          8S@          8@          
                 8@8X        8               8X   8@8       8       8X8X8@8X8               8X       8               @       X8t   XXSXSX%SS@S88             tXS                           S% 888S                      S              %                 X    @S       8SS          8X@         8X          
                   8X8X      8               8X   8@8       8@8X8X8X8%XXX@XX888             8X8      8               8       @XS     X@SX8%8;8 :SSX%SSX%S%XtXS:8 %                      :X S 888                        S                S              SSX   X S      @ S            8S        8X          
                     8X8     8               8X     8@8     8                 8@8            888     8               8     88S        @8%8                      S8:%                 ;S:@ 8 88                           X                 S           SSX     S%XS    XSX              8 X     8           
                       8@8   8               88      8@8X   8                  8@8X            8@8   8               8   88S8           8S8@                      SXS                tXS 8 8                               S                          XX         %X    SSX               @ S    @S          
                        8@8X 8               8@        8X8  8                    8X8             8@8 8               8  888               8X8                       XXX                %8                                   .S                      X%              S   S                  XSX X S          
                          8X8X               8X          8@8X                     8X8X            8@8@               8@8@                   8@8                      @8SX                                                    :X S               S S%%                X @%8                   S @8           
                            8S8X8@8X8X8@8X8X8S@           8X8X8X8@8X8X8@8X8X8@8X8X8@@@8S            8S8@8X8X8@8X8X8@8SX                      888X8X8@8X8@8X8@8X8@8@88S@8XS                                                     ;S S.S S S % S S X S                    XS                     %  S          
                                                                                                                                                                                                                                                                         X                       S          
                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                            

ZGo (part 2): Calling Go from Zig

===============================


Following our last post Calling Zig from Go, we can also utilize Zig to call Go. There are two possible approaches here, using go tool to generate C and importing it in Zig, or building a library and linking it with Zig. I opted for the library route from Go. It should be a more general use-case that can be applied to other languages as well.


Environment Assumptions:

*************************


  • Golang 1.18+ [>]
  • Zig 0.10.0-dev+ [>]
  • An x86_64 architecture.











Files:

******

                                                            
                                                            
                        8@8888@88888%                       
                    X88X8@XXXX@XX%8888888  88               
             @888@88X8888@8X@8@X%@%. t@88@88888             
            8X888@8S     .: 88X::S      ;8@88@S@            
            8: 8@8 :8@@  . .@88%8t8     .8X%:@S8            
             888@8 S.;@    .88@8 ;;  .  ;88888              
              @888X;:: .  t 8888S. .  @S8@@X8:              
              88X8@8%@@8X@88t;t;88Xt8@8X8SX888              
              X@@88@88888@;X;@ X8S@8@8SX8@888@              
              8@88888X8888 :%S SS;@@X@@8888888              
              @@X88888@8X8@;. @%;8X@88888888S8              
              8X88S8S88S@8@8 @ X8X@SX88888888SX             
              8888888X8888X888@SX8@@888888@8888             
              SX@8S8S88@S8S8888XSX888@8@@88X8S@             
              @X@8S88888S888X888@88:888X88888             
              8@S88tt8XX ;;X8@@@8X:8 X8S888X8X8             
           ; SXX@@8@: @8:XXX 88888.tX8 @8X888SS8X@          
            S8 XX88;8 8t8SSX8. 8@%88X88888888888X8          
              @X888@. 8@X8@88@8 X88XS  8@8888@@             
              8%888S8 @8@8%8XX@X@@S 8 888S888             
              8S@88t.8SSS%:.8%% ;SS@8 .88888888             
              8X88S888:.@ .@ 8:t8;Xt.8X%8S8S8tt             
              8X8SXS;XX 8:.     S@;:.  @S8S888@             
              8X8888X: %8X@S%X@8SS%X%St8@888S8:             
              888888X@@@X;8%8X8@St8t8;8@X888888             
              X88888@8SXSSt8@88XS%8@@XSS8S8888              
               8X88S@88%8SX8S88SX88X%8@8S8%8S8              
               8@X888@8S888S88888X@@888888888               
                88X88888888X888S88@888888S8;                
                   8X88888X888S8888@88@88 .                 
               X 88; 88@8888888X8S88888  .@;8               
               X8X8       8@X%888X        ;88               
                                                            
                                                            

  • hello.go
  • package main
    
    import "fmt"
    import "C"
    
    func main() {
    }
    
    //export HelloGopher
    func HelloGopher() {
            fmt.Printf("Hello Gopher")
    }
    
    Here we need the comment decoration to export our HelloGopher symbol for Zig to use.


  • hello.zig
  • const std = @import("std");
    pub const go = @cImport({
        @cInclude("libhello.h");
    });
    
    const stdout = std.io.getStdOut().writer();
    
    pub fn main() !void {
        try stdout.print("Hello Ziguana\n", .{});
        go.HelloGopher();
    }
    
    Using the @cImport and @cInclude built-ins we can import the header file autogenerated by the Go c-archive build process. This translates the header from C to Zig, to see what is generated by this process you can run zig translate-c libhello.h after the build process.


  • build.zig
  • const std = @import("std");
    const Builder = std.build.Builder;
    
    pub fn build(b: *Builder) void {
        // Standard target options allows the person running `zig build` to choose
        // what target to build for. Here we do not override the defaults, which
        // means any target is allowed, and the default is native. Other options
        // for restricting supported target set are available.
        const target = b.standardTargetOptions(.{});
    
        // Standard release options allow the person running `zig build` to select
        // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
        const mode = b.standardReleaseOptions();
    
        const exe = b.addExecutable("hello", "hello.zig");
        exe.linkLibC();
        exe.linkSystemLibrary("hello");
        exe.addIncludeDir("./");
        exe.addLibraryPath("./");
        exe.setTarget(target);
        exe.setBuildMode(mode);
        exe.install();
    
        const go = build_go(b);
        const make_step = b.step("go", "Make go executable");
        make_step.dependOn(&go.step);
    
        const run_cmd = exe.run();
        run_cmd.step.dependOn(b.getInstallStep());
    
        const run_step = b.step("run", "Run the app");
        run_step.dependOn(&run_cmd.step);
    }
    
    fn build_go(b: *std.build.Builder) *std.build.RunStep {
    
        const go = b.addSystemCommand(
            &[_][]const u8{
                "go",
                "build",
                "-buildmode",
                "c-archive",
                "-o",
                "libhello.a",
                "hello.go",
            },
        );
        return go;
    }
    

    There is not much that has changed from our last build.zig, other than the steps being reversed. We need to call the go step before our default step so we can link the produced library with our Zig program. Note that for the library to be found it must be named libhello.a and be produced in our defined Library search path (the current directory.)

    Running zig build go && zig build will compile the executable.


    Calling Go!
    linux@solar ~/P/goz> ./zig-out/bin/hello
    Hello Ziguana
    Hello Gopher


    Conclusion:

    *************


    Zig and Go interop feels painless. Both are innovative languages with different but compatible goals. Go has the advantage of a huge ecosystem of libraries and being able to tap into that with Zig seamlessly is a powerful tool. There has been a handful of times when I wanted to vendor a library that simply did not exist in C, and having Go as an additional option will help cover more bases.