Skip to content

cmd/cgo: multiple assignment to get errno value does not work with 'var' #13930

Closed
@yulvil

Description

@yulvil

According to the cgo documentation: "Any C function (even void functions) may be called in a multiple assignment context to retrieve both the return value (if any) and the C errno variable as an error"

I was expecting var res, err = C.sqrt(1) to work but only res, err := C.sqrt(1) works.

var res, err = C.sqrt(1) fails at compile time with "assignment count mismatch: 2 = 1"

go version go1.6beta1 linux/amd64

Operating System (Virtual Box Guest): Linux 3.16.0-4-amd64 SMP Debian 3.16.7-ckt20-1+deb8u1 (2015-12-14) x86_64 GNU/Linux

Ref golang-nuts: https://groups.google.com/forum/#!topic/golang-nuts/g88yZMHL_A0

main.go

package main

/*
#cgo LDFLAGS: -lm
#include <math.h>
*/
import "C"

import "fmt"

func main() {
   res, err := C.sqrt(1)        // works
   // var res, err = C.sqrt(1) // assignment count mismatch: 2 = 1
   fmt.Println(res, err)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions