Created
June 3, 2024 10:36
-
-
Save OmkarKirpan/a65d4235d23a8cdc92c4f0a0f65b1d45 to your computer and use it in GitHub Desktop.
Revisions
-
OmkarKirpan created this gist
Jun 3, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() {}