Spacing Issue when using fmt.Sprintf to create a command

Your code is very, very strange. Maybe because you are converting it from C. To begin with,

type Edu_db struct {
    School [4 + 1]byte
    PU     [3 + 1]byte
    BE     [3 + 1]byte
    Mtech  [31 + 1]byte
}

This is not how things are done in Go. You might want byte slices ([]byte), probably actually strings but certainly not byte arrays. You appear to add an extra byte for zero termination? Strings are not zero terminated in Go.

Then,

school := "LFPS"
copy(db.School[:], school)

Your db.School is now {'L', 'F', 'P', 'S', \0} because it’s a [5]byte, initialized to all zeroes, and you copied in four bytes.

Then,

db_str := fmt.Sprintf("School='%s' ...", db.School)

which becomes School='LFPS\0' ... because of the above and of course breaks in C land.

In short, try this:

type Edu_db struct {
    School string
    PU     string
    BE     string
    Mtech  string
}

And, more importantly, make sure to actually learn Go before trying to interface it with C. There is trickiness in the interface, and it depends on the implementation details of both C and Go. You need to understand both.

2 Likes