answerstu

go - What is wrong with my systemd service

I have written a golang RestAPI based on labstack/echo and vuejs and have a working version compiled and everything runs nicely when I start it. So far so good.However when trying to integrate it with systemd to start the process at boot I am stuck. I have a service file.[Unit]Description=Server Software ManagerAfter=network.target[Service]Type=simpleExecStart=/var/gameserver/steam/smanKillMode=processUser=steamGroup=steamRestart=on-failureSuccessExitStatus=2[Install]WantedBy=multi-user.targetAlias=sman.serviceBut everytime I want to start the ...Read more

go - Read preferring RW mutex lock in Golang

I need a read preferring RW mutex in golang. Is there a package in golang that will satisfy my needs. I tried sync.RWMutex, but it seems to be write preferring lock. Here goes my attempt to distinguish Go's RWMutex,package mainimport ( "fmt" "sync" "time")func main() {y := &resource{x: 10}go func() { defer fmt.Println("done first read") y.RLock() defer y.RUnlock() go func() { defer fmt.Println("done first write") fmt.Println("first write req") y.Lock() fmt.Println("after first write granted")...Read more

How can I call a Rust function from Go with a slice as a parameter?

I want to call some external functions written in Rust from Go with a reference to a slice.I have the following Rust code:extern crate libc;#[no_mangle]pub extern "C" fn callme(data: &mut [libc::c_double]) -> i32 { data.len() as i32}This function is made available for the cgo compiler through this C-style header file:#IFNDEF BOGUSLIB_H#DEFINE BOGUSLIB_Hextern int callme(double* data); #ENDIFI can now call this function from Go with the Rust crate compiled as a cdylib://#cgo CFLAGS: -Ipath/to/libfolder//#cgo LDFLAGS: -Lpath/to/libf...Read more

go - openapi spec validation in Golang

I want to validate an openapi spec in a similar fashion as its done here : http://bigstickcarpet.com/swagger-parser/www/index.html but the difference is that Ill use GO to code the tool and its only a CLI.I am trying to use this :https://github.com/go-openapi/validateBut the main problem is that the documentation is almost non existent. I came here looking for help of someone that might previously used this library and can give me a MINIMAL example of sending a file containing a spec like such and having this library to throw all the errors or ...Read more

go - RSA Key Export and Import

I'm currently trying to export my created keys and than importing them to use them.But if I run my code I get the following error:panic: x509: only RSA and ECDSA public keys supportedgoroutine 1 [running]:main.main() /path/to/project/src/main.go:19 +0x3bdThis is my current code:// Create keykey, _ := rsa.GenerateKey(rand.Reader, 2048)// Message to encryptmessage := "hi stackoverflow"priv := x509.MarshalPKCS1PrivateKey(key)pub, err := x509.MarshalPKIXPublicKey(key.PublicKey)if err != nil { panic(err)}private, err := x509.ParsePKCS1PrivateK...Read more

go - What are Parse Html token methods for email templates in golang?

I am trying to create email templates having html tokens in golang. I have searched all over the web and found"html/template"library. It supports token format like below Hello {{.Name}} <a href="{{.URL}}">Confirm email address</a>But the requirement for html token is something likeName: {{ test.name }}Phone: {{ test.phone }}Address: {{ test.address }}, {{ test.city }}, {{ test.state }} {{ test.zip }}I could not found such token system in golang or any library supporting such format. Can anyone please tell how can I achieve to crea...Read more

go - []byte to []int or []bool

Let's say I have a []byte and for the sake of making my life easier I want to convert it in a []int or []bool in the following way: Let's say I start with []byte{0x41}, that is 0b01000001. Now, what I would like to obtain is something like:[]int{0,1,0,0,0,0,0,1}or[]bool{f,t,f,f,f,f,f,t}I guess I could cycle with something like this:mybyte & (1 << pos)but I was looking for a more compact approach....Read more

Slices and multiple assignment in the Go Programming Language

According to the "Assignments" section of the Go Language Spec: The assignment proceeds in two phases. First, the operands of index expressions and pointer indirections (including implicit pointer indirections in selectors) on the left and the expressions on the right are all evaluated in the usual order. Second, the assignments are carried out in left-to-right order.While playing with slices and attempting to implement a simple "delete" (or "splice") operation I see the following confusing behavior (Go Playground):xs := []int{1, 2, 3, 4, 5}x ...Read more

go - Deleting element of slice modifies original value

I'm wondering about the following behavior:sl1 := []int{0, 1, 2, 3, 4, 5}fmt.Printf("sl1: %v\n", sl1)//Prints sl1: [0 1 2 3 4 5]idx := 3sl2 := append(sl1[:idx], sl1[idx+1:]...)fmt.Printf("sl1: %v\n", sl1)//Prints sl1: [0 1 2 4 5 5] -> strange!fmt.Printf("sl2: %v\n", sl2)//Prints sl2: [0 1 2 4 5] -> expected!Looks like append is doing something weird on the original slice pointer.Is this a bug or intended behavior? See also https://play.golang.org/p/EX3eqJz5Q8K...Read more

go - How to create a thread-safe rand.Source?

The documentation for the standard-library rand.NewSource function states that, Unlike the default Source used by top-level functions, this source is not safe for concurrent use by multiple goroutines.Is there a way to create a thread-safe rand.Source?Context: I have a type with a method that may be called by multiple go-routines, and this method uses random numbers. I want to support dependency injection for testing, so I can't use the default Source....Read more

go - Using exec only starts program in background

I am trying to make a go program that calls another program (with a GUI) by command line.Using os/exec package. The external program starts, but it always runs in the background. (Even if I try to start "Notepad.exe" or a .bat file which then calls the actual program.) In the task manager you can see the process but there no way to interact with it.Here's an example of code I've been experimenting with:cmd := exec.Command("cmd.exe", "/C", "start", "\"\"", `Notepad.exe`)log.Println("cmd.exe", "/C", "start", "\"\"", `Notepad.exe`)cmd.Stdout = os....Read more

go - golang package exec issues

I am having trouble using the go exec package.I am receiving this error : '\"C:\Program Files\Sublime Text 3\sublime_text.exe\"' is not recognized as an internal or external command,operable program or batch file.When I try to run this code in golang on a Windows machine: package mainimport ("os/exec""fmt")func main() { var CMD string = "\"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\" C:\\Users\\User\\Desktop\\Guess.py" //var CMD string = `"C:\Program Files\Sublime Text 3\sublime_text.exe" C:\Users\User\Desktop\Guess.py` ExecCMD := exec...Read more

go - golang, run with daemonize

After build to file. I run it by daemonize with commanddaemonize /var/www/repo/bin/tracking_api 1>> /var/www/repo/bin/tracking_api.logI see it generate a new file but nothing in that file. In code I log.Print a lot. I just know basic about GO, please guide me to log that. Is there any easier way to run golang to server instead of using daemonize?...Read more

go - How do I register for systemd service lifecycle events?

I want to be informed about all state changes of service units. I know there is the DBUS API which gives me signals for UnitNew and UnitRemoved, but the semantic is still unclear to me. Also the signals doesn't provide the informations I need.I'm using go and tried the go-systemd lib from coreos. They provide a way to "subscribe" but it's in fact polling all the unit information available in the system. I do not want to do so every few milliseconds, just to get informed about statechanges.I also tried the default org.freedesktop.DBus.Properties...Read more

go - How to use a reflect.Type to perform a type assertion

I know reflection is generally frowned upon in go but for my current purposes Im pretty sure it is the best solution.Essentially my project is cli tool which will output an xml query based on incoming commands and return the corresponding result.There is some boiler plate code for each command request where default values are populated and supplied values validated.So I have a series of Command objects based on a Command struct as follows:type Command struct { Name string Request interface{} RequestType reflect.Type Respo...Read more