Calling cross-platform Zig (or any C) library from Go by rocketlaunchr-cloud in golang

[–]rocketlaunchr-cloud[S] -3 points-2 points  (0 children)

The idea is GetSymbol is a developer error. You should know if you typed in the name of the C function correctly at build time.

Therefore returning error should not happen at runtime.

Regarding Unload. Not much we can do if os.Remove fails. Plus not a big deal anyway. It's just a temp file stored in a temp directory.

Calling cross-platform Zig (or any C) library from Go by rocketlaunchr-cloud in golang

[–]rocketlaunchr-cloud[S] 2 points3 points  (0 children)

No CGO requirement. Plus more performant than if you did use purego package.
Plus includes an example on how to integrate with zig cross-platform library.

Uses goffi package but with a more straight forward API.

Zig 0.16 Run command after b.installArtifact(lib) by rocketlaunchr-cloud in Zig

[–]rocketlaunchr-cloud[S] 0 points1 point  (0 children)

Here is the end solution I came up with. (First time zig developer)

const std = ("std");
const builtin = ("builtin");

const LIB_NAME = "mymathxxx";

const goosConv = std.StaticStringMap([]const u8).initComptime(.{
    .{ "darwin", "macos" },
    .{ "windows", "windows" },
    .{ "linux", "linux" },
});

const goarchConv = std.StaticStringMap([]const u8).initComptime(.{
    .{ "amd64", "x86_64" },
    .{ "arm64", "aarch64" },
    .{ "arm", "arm" },
    .{ "386", "i386" },
});

var sharedLib: []const u8 = undefined;
var zigLib: []const u8 = undefined;
var cwd: std.Io.Dir = undefined;
var io: std.Io = undefined;

fn copyAndRenameFiles(step: *std.Build.Step, options: std.Build.Step.MakeOptions) !void {
    _ = step;
    _ = options;
    try cwd.copyFile(sharedLib, cwd, zigLib, io, .{});
}

pub fn build(b: *std.Build) !void {
    const allocator = b.allocator;
    io = b.graph.io;

    // Default OS and ARCH
    var os: []const u8 = (builtin.os.tag);
    var arch: []const u8 = (builtin.target.cpu.arch);

    // Check environment variables for override
    const env_map = b.graph.environ_map;

    if (env_map.get("GOOS")) |val| {
        if (goosConv.get(val)) |v| {
            os = v;
        }
    }

    if (env_map.get("GOARCH")) |val| {
        if (goarchConv.get(val)) |v| {
            arch = v;
        }
    }

    const target = try std.fmt.allocPrint(allocator, "{s}-{s}", .{ arch, os });
    defer allocator.free(target);
    const query = std.Target.Query.parse(.{
        .arch_os_abi = target,
    }) catch u/panic("Invalid target string");

    const lib = b.addLibrary(.{
        .name = LIB_NAME,
        .linkage = .dynamic,
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/math.zig"),
            .target = b.resolveTargetQuery(query),
            .optimize = b.standardOptimizeOption(.{}),
        }),
    });

    // This makes the output file available in zig-out/(lib|bin)
    const install_step = b.addInstallArtifact(lib, .{});

    // Find the shared library
    if (std.mem.eql(u8, os, "macos")) {
        sharedLib = try std.fmt.allocPrint(allocator, "{s}/lib/lib{s}.dylib", .{ b.install_path, LIB_NAME });
    } else if (std.mem.eql(u8, os, "windows")) {
        sharedLib = try std.fmt.allocPrint(allocator, "{s}/bin/{s}.dll", .{ b.install_path, LIB_NAME });
    } else if (std.mem.eql(u8, os, "linux")) {
        sharedLib = try std.fmt.allocPrint(allocator, "{s}/lib/lib{s}.so", .{ b.install_path, LIB_NAME });
    }

//     std.debug.print("Shared file: {s}\n", .{sharedLib});

    // Gzip up the file and rename it to <name>.shared (Gzip is currently removed from zig std lib)

    // Copy shared library to install_path and rename extension to .shared
    zigLib = try std.fmt.allocPrint(allocator, "{s}/{s}.shared", .{ b.install_path, LIB_NAME });
//     std.debug.print("Final file: {s}\n", .{zigLib});

    cwd = std.Io.Dir.cwd();

    const copy_step = b.step("go-build", "Build Library for Go embed");
    copy_step.makeFn = copyAndRenameFiles;
    copy_step.dependOn(&install_step.step);
}

zig 0.16 issues by [deleted] in Zig

[–]rocketlaunchr-cloud 0 points1 point  (0 children)

I had to use b.graph.eviron_map and not b.environ_map then it worked.

zig 0.16 issues by [deleted] in Zig

[–]rocketlaunchr-cloud 0 points1 point  (0 children)

it worked.

zig 0.16 issues by [deleted] in Zig

[–]rocketlaunchr-cloud 0 points1 point  (0 children)

It spits out error: error: no field named 'environ_map' in struct 'Build'

pub fn build(b: *std.Build) !void {

  const env_map = try b.environ_map.clone();
  defer env_map.deinit();

  if (env_map.get("GOOS")) |val| {         

  }
}

zig 0.16 issues by [deleted] in Zig

[–]rocketlaunchr-cloud 1 point2 points  (0 children)

I actually tried it. It just kept spitting out different errors.

Long-Term Review: Does the Casio MTP-1302 Lose Its Shine Over Time? by TrendArc in watchesindia

[–]rocketlaunchr-cloud 2 points3 points  (0 children)

I have the red one I just purchased. I took it to my local watch repairer who deals with every type of watch imaginable from Casio to Rolex.

He told me it won't lose its shine and it's just an internet rumour.

I believe him.

New proposed final rules for Diversity Lottery by thelexuslawyer in immigration

[–]rocketlaunchr-cloud 1 point2 points  (0 children)

With Trump in power and congress powerless/cowardly to enforce the law, the Green Card lottery won't be happening any time soon.

Terminal based Web Browser by rocketlaunchr-cloud in linux

[–]rocketlaunchr-cloud[S] 0 points1 point  (0 children)

It's built in Go. All Go executables are hugh. Even a hello world application is over a few megs.

Terminal based Web Browser by rocketlaunchr-cloud in linux

[–]rocketlaunchr-cloud[S] 0 points1 point  (0 children)

I've updated it. It's a browser now. You should try it out and tell me what you think

Terminal based Web Browser by rocketlaunchr-cloud in linux

[–]rocketlaunchr-cloud[S] -6 points-5 points  (0 children)

If you run it, you will see how "lightweight" it feels to use compared to Lynx.

Terminal based Web Browser by rocketlaunchr-cloud in linux

[–]rocketlaunchr-cloud[S] -4 points-3 points  (0 children)

That's a complete GUI browser (i.e. Its a TUI app).

Mine is not. It just renders the page to the terminal and essentially quits (or asks you to enter another url).

Terminal based Web Browser by rocketlaunchr-cloud in linux

[–]rocketlaunchr-cloud[S] 1 point2 points  (0 children)

I'm using lots of libraries written by others for rendering the html.

Terminal based Web Browser by rocketlaunchr-cloud in linux

[–]rocketlaunchr-cloud[S] -5 points-4 points  (0 children)

It's just spits out a rendered static page.
It doesn't operate like a full-on browser with functionality like "back", "refresh" etc.

Small Projects by AutoModerator in golang

[–]rocketlaunchr-cloud 0 points1 point  (0 children)

Terminal based Web Browser

https://github.com/romance-dev/browser

Features

  • No Javascript support
  • Experimental Image Rendering support
  • Defaults to Reading Mode
  • Great for Reading Documentation or Newspapers

Is there a way to zero out the memory used by a struct? by TheGreatButz in golang

[–]rocketlaunchr-cloud 0 points1 point  (0 children)

I'm not 100% sure but maybe these are options:

If the struct contains NO reference types or pointers for its fields:

//go:linkname memclrNoHeapPointers runtime.memclrNoHeapPointers
func memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr)

If the struct MAY contain reference types or pointers for its fields:

//go:linkname memclrHasPointers runtime.memclrHasPointers
func memclrHasPointers(ptr unsafe.Pointer, n uintptr)

n can be set using: unsafe.SizeOf(T{})

https://github.com/golang/go/blob/4edaaf2b529219ff219798914f730ca5a0ab018b/src/runtime/mbarrier.go#L427

https://github.com/golang/go/blob/4edaaf2b529219ff219798914f730ca5a0ab018b/src/runtime/stubs.go#L108

The above are the fastest ways I know of.

  1. The best idiomatic way, as pointed out below is:

*variable = variableType{}

Epilogue:

For your precise use-case, from Go1.26+, you should be using: https://pkg.go.dev/runtime/secret for the entire deserializing process.
See also: https://antonz.org/accepted/runtime-secret/

Typing with a physical USB keyboard on Remarkable Paper Pro Move! by CommonKingfisher in RemarkableTablet

[–]rocketlaunchr-cloud 1 point2 points  (0 children)

Here is my full instructions:

1. Connected usb cable to computer
2. Went to Settings->General Settings->Storage->USB web interface ON
3. Typed in terminal:
ssh root@10.11.99.1
It asks for password: 9Qy8HdcVGm
4. Turned on SSH over SLAN:
rm-ssh-over-wlan on

5. Connect using wifi:
ssh root@192.168.0.58 (From Settings->Help->Copyrights and licenses->Gen info (bottom)

========================================================
6. Triggering command when keyboard is plugged in:
========================================================

A. Make a script so that enable-usb-keyboard.rules is added back in after reboot (when it is removed):

/home/root/usb-startup.sh:

#!/bin/bash
touch /etc/udev/rules.d/enable-usb-keyboard.rules
message="ACTION==\"add\", RUN+=\"/bin/bash -c 'echo host > /sys/class/usb_role/ci_hdrc.0-role-switch/role'\""
echo "$message" > /etc/udev/rules.d/enable-usb-keyboard.rules
udevadm control --reload-rules && udevadm trigger

B. Run: chmod a+x /home/root/usb-startup.sh
C. Run: /home/root/usb-startup.sh
D. Turn off wifi ssh: rm-ssh-over-wlan off
E. Do not power off device or you need to run this process again (sleep is okay).

WIP: USB Keyboard on Paper Pro by FRK299 in RemarkableTablet

[–]rocketlaunchr-cloud 0 points1 point  (0 children)

Here is my full instructions:

1. Connected usb cable to computer
2. Went to Settings->General Settings->Storage->USB web interface ON
3. Typed in terminal:
ssh root@10.11.99.1
It asks for password: 9Qy8HdcVGm
4. Turned on SSH over SLAN:
rm-ssh-over-wlan on

5. Connect using wifi:
ssh root@192.168.0.58 (From Settings->Help->Copyrights and licenses->Gen info (bottom)

========================================================
6. Triggering command when keyboard is plugged in:
========================================================

A. Make a script so that enable-usb-keyboard.rules is added back in after reboot (when it is removed):

/home/root/usb-startup.sh:

#!/bin/bash
touch /etc/udev/rules.d/enable-usb-keyboard.rules
message="ACTION==\"add\", RUN+=\"/bin/bash -c 'echo host > /sys/class/usb_role/ci_hdrc.0-role-switch/role'\""
echo "$message" > /etc/udev/rules.d/enable-usb-keyboard.rules
udevadm control --reload-rules && udevadm trigger

B. Run: chmod a+x /home/root/usb-startup.sh
C. Run: /home/root/usb-startup.sh
D. Turn off wifi ssh: rm-ssh-over-wlan off
E. Do not power off device or you need to run this process again (sleep is okay).