Re: [go-nuts] C variadic macro equivalent in Golang?

2019-12-01 Thread minforth
That did it:

// --- variad.go -- variadic function test

package main
import("fmt";"os")

// flags
var df,ef int = 1,1

// variadic function thru empty interface
func PR(f string, a ...interface{}) {
if df!=0 { fmt.Printf(f, a...)
   if ef!=0 { fmt.Fprintf(os.Stderr,f, a...) } }
}

func main() { // should print:  -- -- 1 a 1 1 a 1
   PR("-- ")
   PR("%d a %d ",df,ef)
}

// --- End

Thanks a lot to everybody !!   


Am Sonntag, 1. Dezember 2019 03:00:03 UTC+1 schrieb Bakul Shah:
>
> func PR(f string, a ...interface{}) {
> if d { return }
> fmt.Fprintf(os.Stderr, f, a...)
> }
>
> Add appropriate imports. 
>
>
> On Nov 30, 2019, at 12:08 PM, minf...@arcor.de  wrote:
>
> C allows comma-separated argument lists via variadic macros. Whereas AFAIK 
> golang allows only variadic arguments of the same type.
> (Please excuse if I am wrong, I am considering myself a golang newbie)
>
> C demo program that prints:   -- -- 1 and 1 1 and 1
>
> // ## C variadic macro test
>
> #include 
>
> #define PR(...) if(df){printf(__VA_ARGS__);\
> if(ef)fprintf(stderr,__VA_ARGS__);}
>
> // Flags
> int df=1, ef=1;
>
> int main(void) {
>   PR("-- ");
>   PR("%d and %d ",df,ef);
> }
>
> // ## End
>
> How would one implement an equivalent PR(...) "function" in Go ???
>
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golan...@googlegroups.com .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/e36eb032-ffbc-4b26-8c41-f76aa6dcdd00%40googlegroups.com
>  
> 
> .
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ee90bef1-60c9-4f77-bf74-f9afa5080c22%40googlegroups.com.


Re: [go-nuts] C variadic macro equivalent in Golang?

2019-11-30 Thread Bakul Shah
func PR(f string, a ...interface{}) {
if d { return }
fmt.Fprintf(os.Stderr, f, a...)
}

Add appropriate imports. 


> On Nov 30, 2019, at 12:08 PM, minfo...@arcor.de wrote:
> 
> C allows comma-separated argument lists via variadic macros. Whereas AFAIK 
> golang allows only variadic arguments of the same type.
> (Please excuse if I am wrong, I am considering myself a golang newbie)
> 
> C demo program that prints:   -- -- 1 and 1 1 and 1
> 
> // ## C variadic macro test
> 
> #include 
> 
> #define PR(...) if(df){printf(__VA_ARGS__);\
> if(ef)fprintf(stderr,__VA_ARGS__);}
> 
> // Flags
> int df=1, ef=1;
> 
> int main(void) {
>   PR("-- ");
>   PR("%d and %d ",df,ef);
> }
> 
> // ## End
> 
> How would one implement an equivalent PR(...) "function" in Go ???
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com 
> .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/e36eb032-ffbc-4b26-8c41-f76aa6dcdd00%40googlegroups.com
>  
> .

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/37328E01-C01F-47EE-B4E1-CB7357CD2986%40bitblocks.com.


Re: [go-nuts] C variadic macro equivalent in Golang?

2019-11-30 Thread Michael Jones
You might refer to...

https://blog.learngoprogramming.com/golang-variadic-funcs-how-to-patterns-369408f19085

...to better appreciate Robert's advice. As he suggested, a variadic Go
function accepting a slice of the special, magic "empty interface" type
("interface{}") can indeed do anything. The downside is that you have to do
the anything yourself...validating the types of the unknown and mysterious
arguments at run time rather than the compiler validating it for you at
compile time.

For moral support, fmt.Printf is a popular example of this API structure.

On Sat, Nov 30, 2019 at 4:20 PM  wrote:

> Thanks, but then you would have to define the interface beforehand for any
> argument type. And there are a lot:
> format strings, strings, characters, integers of different size,
> floating-pont numbers, file-ids
>
> Meanwhile I found this discussion:
> https://github.com/golang/go/issues/18605
> which hints that comma-separated variadic arguments might come with Go 2.x
> in some unknown future.
>
>
> Am Samstag, 30. November 2019 22:00:52 UTC+1 schrieb Robert Engels:
>
>> Make the type interface{} and you can pass anything and use reflect
>>
>> On Nov 30, 2019, at 2:08 PM, minf...@arcor.de wrote:
>>
>> 
>> C allows comma-separated argument lists via variadic macros. Whereas
>> AFAIK golang allows only variadic arguments of the same type.
>> (Please excuse if I am wrong, I am considering myself a golang newbie)
>>
>> C demo program that prints:   -- -- 1 and 1 1 and 1
>>
>> // ## C variadic macro test
>>
>> #include 
>>
>> #define PR(...) if(df){printf(__VA_ARGS__);\
>> if(ef)fprintf(stderr,__VA_ARGS__);}
>>
>> // Flags
>> int df=1, ef=1;
>>
>> int main(void) {
>>   PR("-- ");
>>   PR("%d and %d ",df,ef);
>> }
>>
>> // ## End
>>
>> How would one implement an equivalent PR(...) "function" in Go ???
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golan...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/golang-nuts/e36eb032-ffbc-4b26-8c41-f76aa6dcdd00%40googlegroups.com
>> 
>> .
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/181a2df8-f5a2-4a35-9d34-6645d2b1356a%40googlegroups.com
> 
> .
>


-- 

*Michael T. jonesmichael.jo...@gmail.com *

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CALoEmQxDG%2BSXeatp9CRB9O1x6woiGoHsFFYFrxmMaXktYWzpsg%40mail.gmail.com.


Re: [go-nuts] C variadic macro equivalent in Golang?

2019-11-30 Thread minforth
Thanks, but then you would have to define the interface beforehand for any 
argument type. And there are a lot:
format strings, strings, characters, integers of different size, 
floating-pont numbers, file-ids

Meanwhile I found this discussion:
https://github.com/golang/go/issues/18605
which hints that comma-separated variadic arguments might come with Go 2.x 
in some unknown future.


Am Samstag, 30. November 2019 22:00:52 UTC+1 schrieb Robert Engels:

> Make the type interface{} and you can pass anything and use reflect
>
> On Nov 30, 2019, at 2:08 PM, minf...@arcor.de  wrote:
>
> 
> C allows comma-separated argument lists via variadic macros. Whereas AFAIK 
> golang allows only variadic arguments of the same type.
> (Please excuse if I am wrong, I am considering myself a golang newbie)
>
> C demo program that prints:   -- -- 1 and 1 1 and 1
>
> // ## C variadic macro test
>
> #include 
>
> #define PR(...) if(df){printf(__VA_ARGS__);\
> if(ef)fprintf(stderr,__VA_ARGS__);}
>
> // Flags
> int df=1, ef=1;
>
> int main(void) {
>   PR("-- ");
>   PR("%d and %d ",df,ef);
> }
>
> // ## End
>
> How would one implement an equivalent PR(...) "function" in Go ???
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golan...@googlegroups.com .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/e36eb032-ffbc-4b26-8c41-f76aa6dcdd00%40googlegroups.com
>  
> 
> .
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/181a2df8-f5a2-4a35-9d34-6645d2b1356a%40googlegroups.com.


Re: [go-nuts] C variadic macro equivalent in Golang?

2019-11-30 Thread Robert Engels
Make the type interface{} and you can pass anything and use reflect

> On Nov 30, 2019, at 2:08 PM, minfo...@arcor.de wrote:
> 
> 
> C allows comma-separated argument lists via variadic macros. Whereas AFAIK 
> golang allows only variadic arguments of the same type.
> (Please excuse if I am wrong, I am considering myself a golang newbie)
> 
> C demo program that prints:   -- -- 1 and 1 1 and 1
> 
> // ## C variadic macro test
> 
> #include 
> 
> #define PR(...) if(df){printf(__VA_ARGS__);\
> if(ef)fprintf(stderr,__VA_ARGS__);}
> 
> // Flags
> int df=1, ef=1;
> 
> int main(void) {
>   PR("-- ");
>   PR("%d and %d ",df,ef);
> }
> 
> // ## End
> 
> How would one implement an equivalent PR(...) "function" in Go ???
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/e36eb032-ffbc-4b26-8c41-f76aa6dcdd00%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/E0C24B80-31F4-4A6C-AEAD-DB0FA533C595%40ix.netcom.com.