go

Use Go slice in C

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-07 10:36:31
问题 I'm trying to build a go shared library with a function that returns a slice. How can I use the slice from C code ? package main import "C" type T struct { A C.int B *C.char } //export Test func Test() []T { arr := make([]T, 0) arr = append(arr, T{C.int(1), C.CString("a")}) arr = append(arr, T{C.int(2), C.CString("abc")}) return arr } func main() {} go build -o lib.so -buildmode=c-shared main.go I now have a lib.so and a lib.h What would be the C code to print the values of the array ?

How to test the main package in Golang from a “test” package?

南楼画角 提交于 2021-02-07 10:26:36
问题 I have a simple program written in Golang. It's an API. So inside the project folder, there's a folder named cmd containing my main package (used to initialise the app and defines the endpoints for the API). There's also a folder named after my program, containing multiple files from a package also named after my program. This package serves as the model to do all the necessary queries and contains all the types I have defined. I also created a folder called test . It contains all my test

How to test the main package in Golang from a “test” package?

旧时模样 提交于 2021-02-07 10:25:21
问题 I have a simple program written in Golang. It's an API. So inside the project folder, there's a folder named cmd containing my main package (used to initialise the app and defines the endpoints for the API). There's also a folder named after my program, containing multiple files from a package also named after my program. This package serves as the model to do all the necessary queries and contains all the types I have defined. I also created a folder called test . It contains all my test

Building Go apps with private modules in Docker

笑着哭i 提交于 2021-02-07 09:30:37
问题 I'm trying to build a go project in a docker container that relies on private submodules. I was hoping that --mount=type=ssh would pass my ssh credentials to the container and it'd work. Currently I can build locally with just make the GOPRIVATE variable set and the git config update. Here is my relevant Dockerfile currently # syntax = docker/dockerfile:experimental FROM golang:1.14.3-alpine AS build RUN apk add --no-cache git \ openssh-client \ ca-certificates WORKDIR /src ENV GIT_TERMINAL

Parallel Module Deployment using App Engine SDK

坚强是说给别人听的谎言 提交于 2021-02-07 09:29:30
问题 TL;DR Is there a way to deploy App Engine modules in parallel? I've built a go application using Google's App Engine SDK for Go. This application defines multiple modules. These modules are self-contained, and do not require any sort of dependency across other modules. When I attempt to deploy the modules to the Google Cloud, I can't help but notice that the modules are uploaded sequentially. This would be fine if deployment was relatively quick, but each module requires it's own redundant

In Golang, how to receive multicast packets with socket bound to specific address/port?

时光总嘲笑我的痴心妄想 提交于 2021-02-07 09:07:07
问题 Task at hand is to bind a socket specifically to address 1.0.0.2:520 (assigned to eth2), then read multicast UDP packets addressed to 224.0.0.9:520. I am trying the code below, based on https://godoc.org/golang.org/x/net/ipv4 Unfortunately, result is this debbuging message is never reached: log.Printf("udpReader: recv %d bytes from %s to %s on %s", n, cm.Src, cm.Dst, ifname) I know eth2 is receiving the desired packets because I have this packet sniffer running on it: sudo tcpdump -n -i eth2

In Golang, how to receive multicast packets with socket bound to specific address/port?

只谈情不闲聊 提交于 2021-02-07 09:06:56
问题 Task at hand is to bind a socket specifically to address 1.0.0.2:520 (assigned to eth2), then read multicast UDP packets addressed to 224.0.0.9:520. I am trying the code below, based on https://godoc.org/golang.org/x/net/ipv4 Unfortunately, result is this debbuging message is never reached: log.Printf("udpReader: recv %d bytes from %s to %s on %s", n, cm.Src, cm.Dst, ifname) I know eth2 is receiving the desired packets because I have this packet sniffer running on it: sudo tcpdump -n -i eth2

How to print contents of channel without changing it

∥☆過路亽.° 提交于 2021-02-07 08:55:19
问题 I'm writing a program in the Go language, and I have a simple problem: I have some goroutines in my program and channels with which goroutines use to communicate. From time to time I would like to check what is inside the channels. How could I achieve that without interrupting the goroutines' work? Do channels have any function to print their contents? Or should I somehow copy them? var shelf chan int = make(chan int, 5) go Depot(shelf) go Shop(shelf) var input string fmt.Scanln(&input) if

How to print contents of channel without changing it

自古美人都是妖i 提交于 2021-02-07 08:53:03
问题 I'm writing a program in the Go language, and I have a simple problem: I have some goroutines in my program and channels with which goroutines use to communicate. From time to time I would like to check what is inside the channels. How could I achieve that without interrupting the goroutines' work? Do channels have any function to print their contents? Or should I somehow copy them? var shelf chan int = make(chan int, 5) go Depot(shelf) go Shop(shelf) var input string fmt.Scanln(&input) if

unmarshal generic json in Go

你说的曾经没有我的故事 提交于 2021-02-07 08:45:05
问题 I'm a new Go programmer (From Java) and I would like to reproduce a generic way which is esay to use in Java. I want to create some function which allow me to do an Unmarshal on a JSON string in order to avoid code duplicity. This is my current code which is not working : type myStruct1 struct { id string name string } func (obj myStruct1) toString() string { var result bytes.Buffer result.WriteString("id : ") result.WriteString(obj.id) result.WriteString("\n") result.WriteString("name : ")