go - How to ignore fields with sscanf (%* is rejected) -


i wish ignore particular field whilst processing string sscanf.

man page sscanf says

an optional '*' assignment-suppression character: scanf() reads input directed conversion specification, discards input. no corresponding pointer argument required, , specification not included in count of successful assignments returned scanf().

attempting use in golang, ignore 3rd field:

if c, err := fmt.sscanf(str, " %s %d %*d %d ", &iface.name, &iface.btx, &iface.bytesrx); err != nil || c != 3 { 

compiles ok, @ runtime err set to:

bad verb %* integer

golang doco doesn't mention %* conversion specification, say,

package fmt implements formatted i/o functions analogous c's printf , scanf.

it doesn't indicate %* not implemented, so... doing wrong? or has been quietly omitted? ...but then, why compile?

to best of knowledge there no such verb (as format specifiers called in fmt package) task. can however, specifying verb , ignoring value. not particularly memory friendly, though. ideally work:

fmt.scan(&a, _, &b) 

sadly, doesn't. next best option declare variables , ignore 1 don't want:

var a,b,c int fmt.scanf("%d %v %d", &a, &b, &c) fmt.println(a,c) 

%v read space separated token. depending on you're scanning on, may fast forward stream position need scan on. see this answer details on seeking in buffers. if you're using stdio or don't know length input may have, seem out of luck here.

it doesn't indicate %* not implemented, so... doing wrong? or has been quietly omitted? ...but then, why compile?

it compiles because compiler format string string other. content of string evaluated @ run time functions of fmt package. c compilers may check format strings correctness, a feature, not norm. go, go vet command try warn format string errors mismatched arguments.

edit:

for special case of needing parse row of integers , caring of them, can use fmt.scan in combination slice of integers. following example reads 3 integers stdin , stores them in slice named vals:

ints := make([]interface{}, 3) vals := make([]int, len(ints))  i, _ := range ints {     ints[i] = interface{}(&vals[i]) }  fmt.scan(ints...) fmt.println(vals) 

this shorter conventional split/trim/strconv chain. makes slice of pointers each points value in vals. fmt.scan fills these pointers. can ignore of values assigning same pointer on , on values don't want:

ignored := 0  i, _ := range ints {     if(i == 0 || == 2) {         ints[i] = interface{}(&vals[i])     } else {         ints[i] = interface{}(&ignored)     } } 

the example above assign address of ignore values except first , second, ignoring them overwriting.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -