Skip to content

Instantly share code, notes, and snippets.

@OmkarKirpan
Created June 3, 2024 10:36
Show Gist options
  • Select an option

  • Save OmkarKirpan/a65d4235d23a8cdc92c4f0a0f65b1d45 to your computer and use it in GitHub Desktop.

Select an option

Save OmkarKirpan/a65d4235d23a8cdc92c4f0a0f65b1d45 to your computer and use it in GitHub Desktop.

Revisions

  1. OmkarKirpan created this gist Jun 3, 2024.
    55 changes: 55 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    package main

    // #include <stdlib.h>
    import (
    "C"
    "context"
    "encoding/json"
    "fmt"

    "cloud.google.com/go/pubsub"
    )

    //export gpub
    func gpub(msg string) {
    ctx := context.Background()

    // Sets your Google Cloud Platform project ID.
    projectID := "fleet-furnace-197606"

    // Creates a client.
    client, err := pubsub.NewClient(ctx, projectID)
    if err != nil {
    fmt.Printf("Failed to create client: %v", err)
    }
    defer client.Close()

    // Sets the id for the new topic.
    topicID := "okTopic"

    // message := "{'_id':'665ceaade221eb5dc182501d','index':1,'guid':'7177c6c6-babb-4be5-822b-7984c5c3cd93','isActive':true}"
    println("msg:= ", msg)
    message := msg

    data, err := json.Marshal(message)
    if err != nil {
    fmt.Printf("Failed to marshal json")
    }

    fmt.Println(data)
    // Creates the new topic.
    topic := client.Topic(topicID)
    res := topic.Publish(ctx, &pubsub.Message{
    Data: data,
    // Data: []byte("hello world"),
    })

    msgID, err := res.Get(ctx)
    if err != nil {
    fmt.Println(err)
    }

    fmt.Printf("Msg %v created.\n", msgID)
    }

    func main() {}