[go-nuts] How do I set a pkg's version?

2022-11-01 Thread 'Mark' via golang-nuts
I am creating a pkg.
It has a `go.mod` file:
```
module github.com/.../mypkg

go 1.19
```
And code files with functions in .go files all in the mypkg pkg.

How do I specify which version the mypkg pkg is? I know about using `$ git 
tag vX.Y.Z` and `$ git push origin vX.Y.Z`, but is there any way I can have 
this version in a file that can be accessed at build time or runtime?

-- 
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/0a16b738-59e5-4885-90c8-cd168e308623n%40googlegroups.com.


Re: [go-nuts] How do I set a pkg's version?

2022-11-02 Thread 'Mark' via golang-nuts
I hadn't realised about debug.BuildInfo - thanks!

On Tuesday, November 1, 2022 at 9:15:02 PM UTC axel.wa...@googlemail.com 
wrote:

> It feels like an oversight not to mention debug.BuildInfo 
> <https://pkg.go.dev/runtime/debug@go1.19.2#BuildInfo> here. No CI/CD or 
> manual build steps required.
>
> On Tue, Nov 1, 2022 at 9:55 PM Chris Burkert  wrote:
>
>> During CI/CD we create a json file with a few details (git tag, branch, 
>> hash, date, time). Afterwards we compile Go Code which embeds this file 
>> into the binary. During runtime flags like --version print the json.
>>
>> Note that this is about the version of some binary - not the version of a 
>> package. However, you could embed go.mod. But there may be better ways.
>>
>> Hope this helps.
>>
>> 'Mark' via golang-nuts  schrieb am Di. 1. 
>> Nov. 2022 um 16:34:
>>
>>> I am creating a pkg.
>>> It has a `go.mod` file:
>>> ```
>>> module github.com/.../mypkg
>>>
>>> go 1.19
>>> ```
>>> And code files with functions in .go files all in the mypkg pkg.
>>>
>>> How do I specify which version the mypkg pkg is? I know about using `$ 
>>> git tag vX.Y.Z` and `$ git push origin vX.Y.Z`, but is there any way I can 
>>> have this version in a file that can be accessed at build time or runtime?
>>>
>>> -- 
>>> 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...@googlegroups.com.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/golang-nuts/0a16b738-59e5-4885-90c8-cd168e308623n%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/golang-nuts/0a16b738-59e5-4885-90c8-cd168e308623n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> -- 
>> 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...@googlegroups.com.
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/CALWqRZov4r%2BN2FiZug5mmwUwhYcvf08922UQU%3DMqfJKLFT8dBg%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/CALWqRZov4r%2BN2FiZug5mmwUwhYcvf08922UQU%3DMqfJKLFT8dBg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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/ac35d44d-5795-45d3-9b46-5bfd643d2a34n%40googlegroups.com.


[go-nuts] Trying to create a test app for a library package

2022-11-02 Thread &#x27;Mark&#x27; via golang-nuts
I have this layout:
```
mylib/
mylib/go.mod # module github.com/me/mylib
mylib/mylib.go
mylib/mylib_test.go
```
All this works fine, with both .go files being in the same pkg: mylib.

However, there are some tests that I can't really do using the test module 
because they write to stdout. So I want to create an exe for regression 
testing.

In particular I want the regression tester to be in package main (just like 
an app that uses mylib).

I've tried this layout:
```
mylib/
mylib/go.mod # module github.com/me/mylib
mylib/mylib.go
mylib/mylib_test.go
mylib/tester/tester.go
```
But I can't get the import to work:
```go
package main

import (
"fmt"
"mylib"
)

func main() {
parser := mylib.Parser()
fmt.Println(parser.AppName(), parser.Version())
}
```
The error I get is `tester/tester.go:8:2: package garg is not in GOROOT`, 
which is perfectly correct.
So then I tried to change the import to `../mylib`, but that also produces 
an error, `tester/tester.go:8:2: "../mylib" is relative, but relative 
import paths are not supported in module mode`

Is what I'm trying to do possible? If so, how? If not, what d'you recommend?

-- 
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/b9511da6-2e48-44ec-a16a-7985614632f7n%40googlegroups.com.


Re: [go-nuts] How do I set a pkg's version?

2022-11-02 Thread &#x27;Mark&#x27; via golang-nuts
Oh, except that debug.BuildInfo (unsurprisingly) is only available in debug 
not release builds. So I guess the answer is that there isn't any nice way 
to do it.

On Wednesday, November 2, 2022 at 8:31:29 AM UTC Mark wrote:

> I hadn't realised about debug.BuildInfo - thanks!
>
> On Tuesday, November 1, 2022 at 9:15:02 PM UTC axel.wa...@googlemail.com 
> wrote:
>
>> It feels like an oversight not to mention debug.BuildInfo 
>> <https://pkg.go.dev/runtime/debug@go1.19.2#BuildInfo> here. No CI/CD or 
>> manual build steps required.
>>
>> On Tue, Nov 1, 2022 at 9:55 PM Chris Burkert  wrote:
>>
>>> During CI/CD we create a json file with a few details (git tag, branch, 
>>> hash, date, time). Afterwards we compile Go Code which embeds this file 
>>> into the binary. During runtime flags like --version print the json.
>>>
>>> Note that this is about the version of some binary - not the version of 
>>> a package. However, you could embed go.mod. But there may be better ways.
>>>
>>> Hope this helps.
>>>
>>> 'Mark' via golang-nuts  schrieb am Di. 1. 
>>> Nov. 2022 um 16:34:
>>>
>>>> I am creating a pkg.
>>>> It has a `go.mod` file:
>>>> ```
>>>> module github.com/.../mypkg
>>>>
>>>> go 1.19
>>>> ```
>>>> And code files with functions in .go files all in the mypkg pkg.
>>>>
>>>> How do I specify which version the mypkg pkg is? I know about using `$ 
>>>> git tag vX.Y.Z` and `$ git push origin vX.Y.Z`, but is there any way I can 
>>>> have this version in a file that can be accessed at build time or runtime?
>>>>
>>>> -- 
>>>> 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...@googlegroups.com.
>>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/golang-nuts/0a16b738-59e5-4885-90c8-cd168e308623n%40googlegroups.com
>>>>  
>>>> <https://groups.google.com/d/msgid/golang-nuts/0a16b738-59e5-4885-90c8-cd168e308623n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>> -- 
>>> 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...@googlegroups.com.
>>>
>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/golang-nuts/CALWqRZov4r%2BN2FiZug5mmwUwhYcvf08922UQU%3DMqfJKLFT8dBg%40mail.gmail.com
>>>  
>>> <https://groups.google.com/d/msgid/golang-nuts/CALWqRZov4r%2BN2FiZug5mmwUwhYcvf08922UQU%3DMqfJKLFT8dBg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>

-- 
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/09ab0bb7-3c1e-48da-a5c1-79c0d3f1968dn%40googlegroups.com.


[go-nuts] Is it possible to switch on type T in a generic function?

2022-11-08 Thread &#x27;Mark&#x27; via golang-nuts
Given a function:

func F[T comparable](a T) {
}

is it possible to check T's type inside F?

My use case is that I have a function with signature G[T comparable](x []T) 
and inside G I want to sort the elements in slice x where T could be int or 
string.

This arises in a tiny generic set module I've created: 
https://github.com/mark-summerfield/gset
In the String() method I want to return a string with the elements sorted 
(for human readability and for testing convenience); but at the moment I 
can only do this by converting all elements to strings and sorting them 
which isn't good for ints.

-- 
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/e746f065-24fa-474a-90ec-2d8367bf3e3bn%40googlegroups.com.


Re: [go-nuts] Is it possible to switch on type T in a generic function?

2022-11-08 Thread &#x27;Mark&#x27; via golang-nuts
I see that your example works, but I can't see how to adapt it to my use 
case.

type Set[T comparable] map[T]struct{}

func New[T comparable](elements ...T) Set[T] {
set := make(Set[T], len(elements))
for _, element := range elements {
set[element] = struct{}{}
}
return set
}

func (me Set[T]) String() string {
// Want to sort by T < T //
elements := make([]string, 0, len(me))
for element := range me {
elements = append(elements, fmt.Sprintf("%#v", element))
}
sort.Strings(elements)
/
s := "{"
sep := ""
for _, element := range elements {
s += fmt.Sprintf("%s%s", sep, element)
sep = " "
}
return s + "}"
}

On Tuesday, November 8, 2022 at 9:19:48 AM UTC Jan Mercl wrote:

> On Tue, Nov 8, 2022 at 9:53 AM 'Mark' via golang-nuts
>  wrote:
>
> > Given a function:
> >
> > func F[T comparable](a T) {
> > }
> >
> > is it possible to check T's type inside F?
> >
> > My use case is that I have a function with signature G[T comparable](x 
> []T) and inside G I want to sort the elements in slice x where T could be 
> int or string.
>
> https://go.dev/play/p/zxQYVvOMX35 ?
>

-- 
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/577c056e-bf3f-4002-b431-f73aeda75a6dn%40googlegroups.com.


Re: [go-nuts] Is it possible to switch on type T in a generic function?

2022-11-08 Thread &#x27;Mark&#x27; via golang-nuts
Thanks for your help and very interesting ideas. In the end I used this:

type Set[T comparable] map[T]struct{}

func New[T comparable](elements ...T) Set[T] {
set := make(Set[T], len(elements))
for _, element := range elements {
set[element] = struct{}{}
}
return set
}

func (me Set[T]) String() string {
elements := make([]T, 0, len(me))
for element := range me {
elements = append(elements, element)
}
sort.Slice(elements, func(i, j int) bool {
return less(elements[i], elements[j])
})
s := "{"
sep := ""
for _, element := range elements {
s += sep + asStr(element)
sep = " "
}
return s + "}"
}

func asStr(x any) string {
if s, ok := x.(string); ok {
return fmt.Sprintf("%q", s)
}
return fmt.Sprintf("%v", x)
}

func less(a, b any) bool {
switch x := a.(type) {
case int:
return x < b.(int)
case float64:
return x < b.(float64)
case string:
return x < b.(string)
default:
return fmt.Sprintf("%v", a) < fmt.Sprintf("%v", b)
}
}

Interestingly, I couldn't put the asStr() code in the String() function 
since doing so produced this error:

invalid operation: cannot use type assertion on type parameter value 
element (variable of type T constrained by comparable)

Anyway, I'm happy that it all works now. (I know I ought to include every 
int & float32, but this is enough for now).



On Tuesday, November 8, 2022 at 11:29:34 AM UTC rog wrote:

> If you're sure that T is an int or a string, then why not constrain it as 
> such? https://go.dev/play/p/1kT6EacMHco
>
> You could go further and constrain it to allow any type with ordering 
> defined: https://go.dev/play/p/il5koj1RPkh
>
> If you want to allow any kind of comparable key in your set, one could 
> observe that essentially you're trying to solve the same problem that the 
> fmt package is solving when it converts maps to string. It uses the 
> internal fmtsort <https://pkg.go.dev/internal/fmtsort> package, which, as 
> luck would have it, has been factored out into an externally available 
> package <https://pkg.go.dev/github.com/rogpeppe/go-internal/fmtsort>. So 
> you could do this: https://go.dev/play/p/oKTGSm_o22a
>
> To answer the specific question you asked, there is an issue that tracks 
> the ability to do a switch directly on a type parameter: 
> https://github.com/golang/go/issues/45380
>
> But you can also work around the lack of that feature by doing something 
> like this: https://go.dev/play/p/3C2a61Ojbxs
>
> Hope this helps,
>
>   rog.
>
>
> On Tue, 8 Nov 2022 at 08:53, 'Mark' via golang-nuts <
> golan...@googlegroups.com> wrote:
>
>> Given a function:
>>
>> func F[T comparable](a T) {
>> }
>>
>> is it possible to check T's type inside F?
>>
>> My use case is that I have a function with signature G[T comparable](x 
>> []T) and inside G I want to sort the elements in slice x where T could be 
>> int or string.
>>
>> This arises in a tiny generic set module I've created: 
>> https://github.com/mark-summerfield/gset
>> In the String() method I want to return a string with the elements sorted 
>> (for human readability and for testing convenience); but at the moment I 
>> can only do this by converting all elements to strings and sorting them 
>> which isn't good for ints.
>>
>> -- 
>> 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...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/e746f065-24fa-474a-90ec-2d8367bf3e3bn%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/e746f065-24fa-474a-90ec-2d8367bf3e3bn%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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/dea0057c-fd75-4a05-a171-0a77bbd1d051n%40googlegroups.com.


Re: [go-nuts] Is it possible to switch on type T in a generic function?

2022-11-09 Thread &#x27;Mark&#x27; via golang-nuts
Thank you! I've now switched to using any as you suggested & also use 
strings.Builder.

On Wednesday, November 9, 2022 at 4:33:48 PM UTC dun...@harris3.org wrote:

> > Interestingly, I couldn't put the asStr() code in the String() function 
> since doing so produced this error:
> > 
> > invalid operation: cannot use type assertion on type parameter value 
> element (variable of type T constrained by comparable)
>
> You need to convert to "any": https://go.dev/play/p/1pMhs22S-8P
>
> P.S. It would be better to use https://pkg.go.dev/strings#Builder for 
> building a string
>
> On Tuesday, 8 November 2022 at 13:09:51 UTC Mark wrote:
>
>> Thanks for your help and very interesting ideas. In the end I used this:
>>
>> type Set[T comparable] map[T]struct{}
>>
>> func New[T comparable](elements ...T) Set[T] {
>> set := make(Set[T], len(elements))
>> for _, element := range elements {
>> set[element] = struct{}{}
>> }
>> return set
>> }
>>
>> func (me Set[T]) String() string {
>> elements := make([]T, 0, len(me))
>> for element := range me {
>> elements = append(elements, element)
>> }
>> sort.Slice(elements, func(i, j int) bool {
>> return less(elements[i], elements[j])
>> })
>> s := "{"
>> sep := ""
>> for _, element := range elements {
>> s += sep + asStr(element)
>>
>> sep = " "
>> }
>> return s + "}"
>> }
>>
>> func asStr(x any) string {
>> if s, ok := x.(string); ok {
>> return fmt.Sprintf("%q", s)
>> }
>> return fmt.Sprintf("%v", x)
>> }
>>
>> func less(a, b any) bool {
>> switch x := a.(type) {
>> case int:
>> return x < b.(int)
>> case float64:
>> return x < b.(float64)
>> case string:
>> return x < b.(string)
>> default:
>> return fmt.Sprintf("%v", a) < fmt.Sprintf("%v", b)
>> }
>> }
>>
>> Interestingly, I couldn't put the asStr() code in the String() function 
>> since doing so produced this error:
>>
>> invalid operation: cannot use type assertion on type parameter value 
>> element (variable of type T constrained by comparable)
>>
>> Anyway, I'm happy that it all works now. (I know I ought to include every 
>> int & float32, but this is enough for now).
>>
>>
>>
>> On Tuesday, November 8, 2022 at 11:29:34 AM UTC rog wrote:
>>
>>> If you're sure that T is an int or a string, then why not constrain it 
>>> as such? https://go.dev/play/p/1kT6EacMHco
>>>
>>> You could go further and constrain it to allow any type with ordering 
>>> defined: https://go.dev/play/p/il5koj1RPkh
>>>
>>> If you want to allow any kind of comparable key in your set, one could 
>>> observe that essentially you're trying to solve the same problem that the 
>>> fmt package is solving when it converts maps to string. It uses the 
>>> internal fmtsort <https://pkg.go.dev/internal/fmtsort> package, which, 
>>> as luck would have it, has been factored out into an externally 
>>> available package 
>>> <https://pkg.go.dev/github.com/rogpeppe/go-internal/fmtsort>. So you 
>>> could do this: https://go.dev/play/p/oKTGSm_o22a
>>>
>>> To answer the specific question you asked, there is an issue that tracks 
>>> the ability to do a switch directly on a type parameter: 
>>> https://github.com/golang/go/issues/45380
>>>
>>> But you can also work around the lack of that feature by doing something 
>>> like this: https://go.dev/play/p/3C2a61Ojbxs
>>>
>>> Hope this helps,
>>>
>>>   rog.
>>>
>>>
>>> On Tue, 8 Nov 2022 at 08:53, 'Mark' via golang-nuts <
>>> golan...@googlegroups.com> wrote:
>>>
>>>> Given a function:
>>>>
>>>> func F[T comparable](a T) {
>>>> }
>>>>
>>>> is it possible to check T's type inside F?
>>>>
>>>> My use case is that I have a function with signature G[T comparable](x 
>>>> []T) and inside G I want to sort the elements in slice x where T could be 
>>>> int or string.
>>>>
>>>> This arises in a tiny generic set module I've created: 
>>>> https:

[go-nuts] How to read struct field names from an empty slice of structs

2022-11-21 Thread &#x27;Mark&#x27; via golang-nuts
I have a two level general-purpose data structure: a struct containing one 
or more slices of structs.
I want to be able to use reflection to populate this, but as a first step I 
want to extract the 
"table names" (i.e., the names of the fields of the outer struct), and the 
"field names" (the names of each field in each of the inner structs).
Below is as far as I've got: it correctly finds the table names (including 
their tag alternatives), but I can't work out how to access the field 
names...

playground 

-- 
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/c1f828b3-119d-4f78-bd4a-1d0be71d9c1an%40googlegroups.com.


[go-nuts] Re: How to read struct field names from an empty slice of structs

2022-11-21 Thread &#x27;Mark&#x27; via golang-nuts
After struggling, I finally solved it; playground 


On Monday, November 21, 2022 at 10:55:06 AM UTC Mark wrote:

> I have a two level general-purpose data structure: a struct containing one 
> or more slices of structs.
> I want to be able to use reflection to populate this, but as a first step 
> I want to extract the 
> "table names" (i.e., the names of the fields of the outer struct), and the 
> "field names" (the names of each field in each of the inner structs).
> Below is as far as I've got: it correctly finds the table names (including 
> their tag alternatives), but I can't work out how to access the field 
> names...
>
> playground 

-- 
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/e2a46498-7f57-418f-a258-0868fa93f7a5n%40googlegroups.com.


[go-nuts] How to fix an awful marshal reflection hack

2022-11-30 Thread &#x27;Mark&#x27; via golang-nuts
I have this code which works but has a horrible hack:
...
nullable := false
kind := field.Kind() // field's type is reflect.Value
if kind == reflect.Ptr {
// FIXME How can I improve upon this truly awful hack?
switch field.Type().String() {
case "*int", "*int8", "*uint8", "*int16", "*uint16", "*int32", 
"*uint32", "*int64", "*uint64":
kind = reflect.Int
case "*float32", "*float64":
kind = reflect.Float64
}
nullable = true
}
switch kind {
case reflect.Bool:
out.WriteString("bool")
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, 
reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, 
reflect.Uint64:
out.WriteString("int")
case reflect.Float32, reflect.Float64:
out.WriteString("real")
...
if nullable {
out.WriteByte('?')
}
What is the correct way to achieve what I'm aiming for?

-- 
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/5ff4b6f6-405c-4ca5-9299-7c15e1d5c424n%40googlegroups.com.


Re: [go-nuts] How to fix an awful marshal reflection hack

2022-11-30 Thread &#x27;Mark&#x27; via golang-nuts
Yes, I'd already tried that (that's what I started with) and unfortunately 
it doesn't work.

On Wednesday, November 30, 2022 at 3:37:47 PM UTC bse...@computer.org wrote:

> On Wed, Nov 30, 2022 at 5:29 AM 'Mark' via golang-nuts <
> golan...@googlegroups.com> wrote:
>
>> I have this code which works but has a horrible hack:
>> ...
>> nullable := false
>> kind := field.Kind() // field's type is reflect.Value
>> if kind == reflect.Ptr {
>>
>
> This should work:
>
> kind = field.Elem().Kind()
>
>
>  
>
>> // FIXME How can I improve upon this truly awful hack?
>> switch field.Type().String() {
>> case "*int", "*int8", "*uint8", "*int16", "*uint16", "*int32", 
>> "*uint32", "*int64", "*uint64":
>> kind = reflect.Int
>> case "*float32", "*float64":
>> kind = reflect.Float64
>> }
>> nullable = true
>> }
>> switch kind {
>> case reflect.Bool:
>> out.WriteString("bool")
>> case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, 
>> reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, 
>> reflect.Uint64:
>> out.WriteString("int")
>> case reflect.Float32, reflect.Float64:
>> out.WriteString("real")
>> ...
>> if nullable {
>> out.WriteByte('?')
>> }
>> What is the correct way to achieve what I'm aiming for?
>>
>> -- 
>> 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...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/5ff4b6f6-405c-4ca5-9299-7c15e1d5c424n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/5ff4b6f6-405c-4ca5-9299-7c15e1d5c424n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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/c2d154f2-b425-4cc9-a015-af30f4dc9de2n%40googlegroups.com.


Re: [go-nuts] How to fix an awful marshal reflection hack

2022-12-01 Thread &#x27;Mark&#x27; via golang-nuts
Thanks. I've now tried that as follows:

fmt.Printf("@@: %T %v\n", field, field)
kind = field.Type().Elem().Kind()
fmt.Printf("##: %T %v\n", field, field)

In every case the output for kind before and after was identical. 
(Naturally, I tried without the print statements too.) And, of course the 
tests fail. So I'm _still_ using the awful hack!

On Wednesday, November 30, 2022 at 5:30:24 PM UTC bse...@computer.org wrote:

> On Wed, Nov 30, 2022 at 10:17 AM 'Mark' via golang-nuts <
> golan...@googlegroups.com> wrote:
>
>> Yes, I'd already tried that (that's what I started with) and 
>> unfortunately it doesn't work.
>>
>
> It fails if field.Elem() is nil. Try this:
>
>   kind = field.Type().Elem().Kind()
>  
>
>>
>> On Wednesday, November 30, 2022 at 3:37:47 PM UTC bse...@computer.org 
>> wrote:
>>
>>> On Wed, Nov 30, 2022 at 5:29 AM 'Mark' via golang-nuts <
>>> golan...@googlegroups.com> wrote:
>>>
>>>> I have this code which works but has a horrible hack:
>>>> ...
>>>> nullable := false
>>>> kind := field.Kind() // field's type is reflect.Value
>>>> if kind == reflect.Ptr {
>>>>
>>>
>>> This should work:
>>>
>>> kind = field.Elem().Kind()
>>>
>>>
>>>  
>>>
>>>> // FIXME How can I improve upon this truly awful hack?
>>>> switch field.Type().String() {
>>>> case "*int", "*int8", "*uint8", "*int16", "*uint16", "*int32", 
>>>> "*uint32", "*int64", "*uint64":
>>>> kind = reflect.Int
>>>> case "*float32", "*float64":
>>>> kind = reflect.Float64
>>>> }
>>>> nullable = true
>>>> }
>>>> switch kind {
>>>> case reflect.Bool:
>>>> out.WriteString("bool")
>>>> case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, 
>>>> reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, 
>>>> reflect.Uint32, 
>>>> reflect.Uint64:
>>>> out.WriteString("int")
>>>> case reflect.Float32, reflect.Float64:
>>>> out.WriteString("real")
>>>> ...
>>>> if nullable {
>>>> out.WriteByte('?')
>>>> }
>>>> What is the correct way to achieve what I'm aiming for?
>>>>
>>>> -- 
>>>> 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...@googlegroups.com.
>>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/golang-nuts/5ff4b6f6-405c-4ca5-9299-7c15e1d5c424n%40googlegroups.com
>>>>  
>>>> <https://groups.google.com/d/msgid/golang-nuts/5ff4b6f6-405c-4ca5-9299-7c15e1d5c424n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>> -- 
>> 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...@googlegroups.com.
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/c2d154f2-b425-4cc9-a015-af30f4dc9de2n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/c2d154f2-b425-4cc9-a015-af30f4dc9de2n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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/afa700de-926f-42b3-a2cd-18b9c1c12438n%40googlegroups.com.


Re: [go-nuts] How to fix an awful marshal reflection hack

2022-12-01 Thread &#x27;Mark&#x27; via golang-nuts
I tried that and it works in the playground, and I added more types and it 
still works in the playground <https://go.dev/play/p/Yxzj4tAAGhM>.
But in my program it still doesn't work:-( 
The actual code is here tdb-go <https://github.com/mark-summerfield/tdb-go> 
in the file marshal.go from line 133 function marshalTableMetaData().
If you run: go test it all works; but if you replace the call to hack() and 
use nullable as you did in the playground, some of the tests fail.

On Thursday, December 1, 2022 at 9:45:48 AM UTC kortschak wrote:

> On Thu, 2022-12-01 at 00:33 -0800, 'Mark' via golang-nuts wrote:
> > Thanks. I've now tried that as follows:
> >
> > fmt.Printf("@@: %T %v\n", field, field)
> > kind = field.Type().Elem().Kind()
> > fmt.Printf("##: %T %v\n", field, field)
> >
> > In every case the output for kind before and after was identical.
> > (Naturally, I tried without the print statements too.) And, of course
> > the tests fail. So I'm _still_ using the awful hack!
> >
>
> Doesn't this do what you want?
>
> https://go.dev/play/p/7jUw_iW8B_8
>
>

-- 
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/74538c6d-a18a-4ca6-b8a9-4cb4fca44f58n%40googlegroups.com.


Re: [go-nuts] How to fix an awful marshal reflection hack

2022-12-01 Thread &#x27;Mark&#x27; via golang-nuts
The reason there's no nullable in the real code is that it isn't needed 
there: if the field is to a pointer variable (e.g., *string), then I call 
hack() and that adds the '?' to the string so no need for a nullable bool; 
otherwise for non-pointers it falls through to the normal processing. So 
the complete -- and working -- code is in the repo and go test works. But 
replacing the call to hack() with kind = field.Type().Elem().Kind() breaks 
the tests.

On Thursday, December 1, 2022 at 1:09:50 PM UTC Marvin Renich wrote:

> * 'Mark' via golang-nuts  [221201 05:17]:
> > I tried that and it works in the playground, and I added more types and 
> it 
> > still works in the playground <https://go.dev/play/p/Yxzj4tAAGhM>.
> > But in my program it still doesn't work:-( 
> > The actual code is here tdb-go <
> https://github.com/mark-summerfield/tdb-go> 
> > in the file marshal.go from line 133 function marshalTableMetaData().
> > If you run: go test it all works; but if you replace the call to hack() 
> and 
> > use nullable as you did in the playground, some of the tests fail.
>
> You don't show the code that doesn't work (i.e. with nullable). Did you
> make a typo like you did in your code below?
>
> > On Thursday, December 1, 2022 at 9:45:48 AM UTC kortschak wrote:
> > 
> > > On Thu, 2022-12-01 at 00:33 -0800, 'Mark' via golang-nuts wrote:
> > > > Thanks. I've now tried that as follows:
> > > >
> > > > fmt.Printf("@@: %T %v\n", field, field)
> > > > kind = field.Type().Elem().Kind()
> > > > fmt.Printf("##: %T %v\n", field, field)
>
> Note that in both Printf statements, you are using field rather than
> kind. If the two Printf's gave different results, I would consider it a
> compiler bug (a really egregious one!).
>
> > > > In every case the output for kind before and after was identical.
> > > > (Naturally, I tried without the print statements too.) And, of course
> > > > the tests fail. So I'm _still_ using the awful hack!
> > > >
> > >
> > > Doesn't this do what you want?
> > >
> > > https://go.dev/play/p/7jUw_iW8B_8
>
> ...Marvin
>
>

-- 
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/e77369e7-7387-496e-ab02-0f47d5319fd6n%40googlegroups.com.


[go-nuts] Which tool provide's the Go playground's import handling...

2023-01-06 Thread &#x27;Mark&#x27; via golang-nuts
If I visit the Go playground  and change the body of 
`main()` to, say, `fmt.Println("hello", math.Abs(-5))` and then click Run, 
the `import "fmt"` line is _automatically_ corrected to be `import 
(\n\t"fmt"\n\t"math"\n)`. I'd like to be able to use this functionality so 
that I can set my editor to do "fiximports" and then "gofmt" rather than 
just "gofmt" as now. But I don't know what the tool is that does this?

-- 
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/9d6b1181-6332-492b-a9cc-93b8953882bfn%40googlegroups.com.


Re: [go-nuts] Which tool provide's the Go playground's import handling...

2023-01-06 Thread &#x27;Mark&#x27; via golang-nuts
Thanks, that's just what I needed :-)

On Friday, January 6, 2023 at 8:21:02 AM UTC kortschak wrote:

> On Fri, 2023-01-06 at 00:13 -0800, 'Mark' via golang-nuts wrote:
> > If I visit the Go playground and change the body of `main()` to, say,
> > `fmt.Println("hello", math.Abs(-5))` and then click Run, the `import
> > "fmt"` line is _automatically_ corrected to be `import
> > (\n\t"fmt"\n\t"math"\n)`. I'd like to be able to use this
> > functionality so that I can set my editor to do "fiximports" and then
> > "gofmt" rather than just "gofmt" as now. But I don't know what the
> > tool is that does this?
>
> https://pkg.go.dev/golang.org/x/tools/cmd/goimports
>
>

-- 
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/0a0d14e9-b1ed-4bef-8785-5ca29778b332n%40googlegroups.com.


[go-nuts] Doc suggestion (& one const suggestion)

2023-05-02 Thread &#x27;Mark&#x27; via golang-nuts
In the docs file modes are often shown in octal, e.g., `0600`.

Doc suggestion: update octal to use the modern unambiguous format, e.g., 
`0o600`.

Const suggestion: add at least one const to `os`, e.g., 
```go
const ModeUserRW = 0o600
// plus any one or two others that are really common?
```
If the const it added it should be used on the docs rather than raw `0o600`.

-- 
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/a8699162-3256-4d51-bba4-87135e5f1b19n%40googlegroups.com.


[go-nuts] Can a struct be made comparable?

2023-07-13 Thread &#x27;Mark&#x27; via golang-nuts
I have a package which has a function `Do[T comparable](a, b []T) Result`.
I have a struct:
```go
type N struct {
  x int
  y int
  t string
}
```
Is it possible to make `N` comparable; in particular by a field of my 
choice, e.g., `t`?

Or will I have to make, say, `DoFunc(a, b []N, eq func(i, j N) bool) 
Result` with, say,
`func eq(i, j N) { return i.t == j.t }`?

-- 
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/139b01ed-14e7-451b-9b0e-c7d223c678aan%40googlegroups.com.


[go-nuts] Re: Can a struct be made comparable?

2023-07-14 Thread &#x27;Mark&#x27; via golang-nuts
What I really want to do is to be able to diff slices of structs on the 
basis of one single field.
For example, given:
```
type Item struct {
  I int
  S string
}
```
and given `a` and `b` are both of type`[]Item`, I want to diff these slices 
based purely on the `S` field, ignoring the `I` field.

This diff pkg  claims to be 
able to do this (something I'm testing, so I don't know either way yet), 
but in any case, it is incredibly slow.

On Friday, July 14, 2023 at 8:31:39 AM UTC+1 Peter Galbavy wrote:

> As a slight digression - I thought I was going mad, but 'slices' and 
> 'maps' are new :-) Only in 1.21 though...
>
> Well, there is a lot of boiler plate that maps.Keys() will get rid of.
>
> On Thursday, 13 July 2023 at 10:06:01 UTC+1 Brian Candler wrote:
>
>> Structs are already comparable, but all fields must be the same:
>> https://go.dev/play/p/XwhSz4DEDwL
>>
>> I think your solution with function 'eq' is fine.  You can see the same 
>> thing in the standard library in slices.CompactFunc and slices.EqualFunc
>> https://pkg.go.dev/slices#CompactFunc
>> https://pkg.go.dev/slices#EqualFunc
>>
>> For the case of "ordered" rather than "comparable", have a look at 
>> slices.BinarySearchFunc and related functions.
>>
>> On Thursday, 13 July 2023 at 09:29:38 UTC+1 Mark wrote:
>>
>>> I have a package which has a function `Do[T comparable](a, b []T) 
>>> Result`.
>>> I have a struct:
>>> ```go
>>> type N struct {
>>>   x int
>>>   y int
>>>   t string
>>> }
>>> ```
>>> Is it possible to make `N` comparable; in particular by a field of my 
>>> choice, e.g., `t`?
>>>
>>> Or will I have to make, say, `DoFunc(a, b []N, eq func(i, j N) bool) 
>>> Result` with, say,
>>> `func eq(i, j N) { return i.t == j.t }`?
>>>
>>

-- 
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/757d10ef-b748-4145-98f6-24849aaed627n%40googlegroups.com.


[go-nuts] Re: Can a struct be made comparable?

2023-07-14 Thread &#x27;Mark&#x27; via golang-nuts
In fact the diff pkg mentioned above does work but is of no use to me since 
for each change it gives back only the field(s) used, not the original 
structs (or pointers to them), so I can't see any way back to the original 
structs (or their slice indexes).

On Friday, July 14, 2023 at 8:58:41 AM UTC+1 Mark wrote:

> What I really want to do is to be able to diff slices of structs on the 
> basis of one single field.
> For example, given:
> ```
> type Item struct {
>   I int
>   S string
> }
> ```
> and given `a` and `b` are both of type`[]Item`, I want to diff these 
> slices based purely on the `S` field, ignoring the `I` field.
>
> This diff pkg  claims to be 
> able to do this (something I'm testing, so I don't know either way yet), 
> but in any case, it is incredibly slow.
>
> On Friday, July 14, 2023 at 8:31:39 AM UTC+1 Peter Galbavy wrote:
>
>> As a slight digression - I thought I was going mad, but 'slices' and 
>> 'maps' are new :-) Only in 1.21 though...
>>
>> Well, there is a lot of boiler plate that maps.Keys() will get rid of.
>>
>> On Thursday, 13 July 2023 at 10:06:01 UTC+1 Brian Candler wrote:
>>
>>> Structs are already comparable, but all fields must be the same:
>>> https://go.dev/play/p/XwhSz4DEDwL
>>>
>>> I think your solution with function 'eq' is fine.  You can see the same 
>>> thing in the standard library in slices.CompactFunc and slices.EqualFunc
>>> https://pkg.go.dev/slices#CompactFunc
>>> https://pkg.go.dev/slices#EqualFunc
>>>
>>> For the case of "ordered" rather than "comparable", have a look at 
>>> slices.BinarySearchFunc and related functions.
>>>
>>> On Thursday, 13 July 2023 at 09:29:38 UTC+1 Mark wrote:
>>>
 I have a package which has a function `Do[T comparable](a, b []T) 
 Result`.
 I have a struct:
 ```go
 type N struct {
   x int
   y int
   t string
 }
 ```
 Is it possible to make `N` comparable; in particular by a field of my 
 choice, e.g., `t`?

 Or will I have to make, say, `DoFunc(a, b []N, eq func(i, j N) bool) 
 Result` with, say,
 `func eq(i, j N) { return i.t == j.t }`?

>>>

-- 
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/63b52d59-e26e-4ceb-ae66-fa9ee82d303dn%40googlegroups.com.


[go-nuts] Re: Can a struct be made comparable?

2023-07-14 Thread &#x27;Mark&#x27; via golang-nuts
Hi Brian,
Your code certainly identifies the different items.
However, that's not a diff tool in the sense I mean.
Unix diff and tools like it don't just say x[i] != y[i], they find the 
longest common subsequences and in essence produce a series of edit 
commands that would turn slice x into slice y.
There are quite a few go diff tools that will do this, including my own 
 based on Python's 
difflib sequence matcher.
What I want to do is find one that does this for slices of structs where 
only one struct field is considered for comparison purposes.

On Friday, July 14, 2023 at 10:00:34 AM UTC+1 Brian Candler wrote:

> I forgot you wanted generics:
> https://go.dev/play/p/PhGVjsWWTdB
>
> On Friday, 14 July 2023 at 09:47:21 UTC+1 Brian Candler wrote:
>
>> You seem to be saying "if the S field is different then I want to 
>> consider these two structs different, and get pointers to the two structs. 
>> If the S field is the same then I want to skip the pair entirely". Is that 
>> right?
>>
>> The required semantics are not entirely clear, but it sounds like a 
>> handful of lines of code to implement - there's no point importing and 
>> learning a third party library.
>>
>> On the assumption that all the elements to be compared are in 
>> corresponding positions in a and b:
>> https://go.dev/play/p/Y71sLUpftzR
>>
>> On Friday, 14 July 2023 at 09:11:35 UTC+1 Mark wrote:
>>
>>> In fact the diff pkg mentioned above does work but is of no use to me 
>>> since for each change it gives back only the field(s) used, not the 
>>> original structs (or pointers to them), so I can't see any way back to the 
>>> original structs (or their slice indexes).
>>>
>>> On Friday, July 14, 2023 at 8:58:41 AM UTC+1 Mark wrote:
>>>
 What I really want to do is to be able to diff slices of structs on the 
 basis of one single field.
 For example, given:
 ```
 type Item struct {
   I int
   S string
 }
 ```
 and given `a` and `b` are both of type`[]Item`, I want to diff these 
 slices based purely on the `S` field, ignoring the `I` field.

 This diff pkg  claims to 
 be able to do this (something I'm testing, so I don't know either way 
 yet), 
 but in any case, it is incredibly slow.

 On Friday, July 14, 2023 at 8:31:39 AM UTC+1 Peter Galbavy wrote:

> As a slight digression - I thought I was going mad, but 'slices' and 
> 'maps' are new :-) Only in 1.21 though...
>
> Well, there is a lot of boiler plate that maps.Keys() will get rid of.
>
> On Thursday, 13 July 2023 at 10:06:01 UTC+1 Brian Candler wrote:
>
>> Structs are already comparable, but all fields must be the same:
>> https://go.dev/play/p/XwhSz4DEDwL
>>
>> I think your solution with function 'eq' is fine.  You can see the 
>> same thing in the standard library in slices.CompactFunc and 
>> slices.EqualFunc
>> https://pkg.go.dev/slices#CompactFunc
>> https://pkg.go.dev/slices#EqualFunc
>>
>> For the case of "ordered" rather than "comparable", have a look at 
>> slices.BinarySearchFunc and related functions.
>>
>> On Thursday, 13 July 2023 at 09:29:38 UTC+1 Mark wrote:
>>
>>> I have a package which has a function `Do[T comparable](a, b []T) 
>>> Result`.
>>> I have a struct:
>>> ```go
>>> type N struct {
>>>   x int
>>>   y int
>>>   t string
>>> }
>>> ```
>>> Is it possible to make `N` comparable; in particular by a field of 
>>> my choice, e.g., `t`?
>>>
>>> Or will I have to make, say, `DoFunc(a, b []N, eq func(i, j N) bool) 
>>> Result` with, say,
>>> `func eq(i, j N) { return i.t == j.t }`?
>>>
>>

-- 
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/51736175-1ecf-406b-acd4-29414f593d13n%40googlegroups.com.


[go-nuts] Re: Can a struct be made comparable?

2023-07-14 Thread &#x27;Mark&#x27; via golang-nuts
Hi Brian,
Ah, thank you, that helped a lot.

On Friday, July 14, 2023 at 11:54:51 AM UTC+1 Brian Candler wrote:

> The 'diff' package you showed identifies the changed object index by means 
> of a "Path" attribute in the Changelog entry. If the top level object is a 
> slice, then Atoi(change.Path[0]) is the index into the slice.
>
> On Friday, 14 July 2023 at 10:47:47 UTC+1 Mark wrote:
>
>> Hi Brian,
>> Your code certainly identifies the different items.
>> However, that's not a diff tool in the sense I mean.
>> Unix diff and tools like it don't just say x[i] != y[i], they find the 
>> longest common subsequences and in essence produce a series of edit 
>> commands that would turn slice x into slice y.
>> There are quite a few go diff tools that will do this, including my own 
>>  based on 
>> Python's difflib sequence matcher.
>> What I want to do is find one that does this for slices of structs where 
>> only one struct field is considered for comparison purposes.
>>
>> On Friday, July 14, 2023 at 10:00:34 AM UTC+1 Brian Candler wrote:
>>
>>> I forgot you wanted generics:
>>> https://go.dev/play/p/PhGVjsWWTdB
>>>
>>> On Friday, 14 July 2023 at 09:47:21 UTC+1 Brian Candler wrote:
>>>
 You seem to be saying "if the S field is different then I want to 
 consider these two structs different, and get pointers to the two structs. 
 If the S field is the same then I want to skip the pair entirely". Is that 
 right?

 The required semantics are not entirely clear, but it sounds like a 
 handful of lines of code to implement - there's no point importing and 
 learning a third party library.

 On the assumption that all the elements to be compared are in 
 corresponding positions in a and b:
 https://go.dev/play/p/Y71sLUpftzR

 On Friday, 14 July 2023 at 09:11:35 UTC+1 Mark wrote:

> In fact the diff pkg mentioned above does work but is of no use to me 
> since for each change it gives back only the field(s) used, not the 
> original structs (or pointers to them), so I can't see any way back to 
> the 
> original structs (or their slice indexes).
>
> On Friday, July 14, 2023 at 8:58:41 AM UTC+1 Mark wrote:
>
>> What I really want to do is to be able to diff slices of structs on 
>> the basis of one single field.
>> For example, given:
>> ```
>> type Item struct {
>>   I int
>>   S string
>> }
>> ```
>> and given `a` and `b` are both of type`[]Item`, I want to diff these 
>> slices based purely on the `S` field, ignoring the `I` field.
>>
>> This diff pkg  claims 
>> to be able to do this (something I'm testing, so I don't know either way 
>> yet), but in any case, it is incredibly slow.
>>
>> On Friday, July 14, 2023 at 8:31:39 AM UTC+1 Peter Galbavy wrote:
>>
>>> As a slight digression - I thought I was going mad, but 'slices' and 
>>> 'maps' are new :-) Only in 1.21 though...
>>>
>>> Well, there is a lot of boiler plate that maps.Keys() will get rid 
>>> of.
>>>
>>> On Thursday, 13 July 2023 at 10:06:01 UTC+1 Brian Candler wrote:
>>>
 Structs are already comparable, but all fields must be the same:
 https://go.dev/play/p/XwhSz4DEDwL

 I think your solution with function 'eq' is fine.  You can see the 
 same thing in the standard library in slices.CompactFunc and 
 slices.EqualFunc
 https://pkg.go.dev/slices#CompactFunc
 https://pkg.go.dev/slices#EqualFunc

 For the case of "ordered" rather than "comparable", have a look at 
 slices.BinarySearchFunc and related functions.

 On Thursday, 13 July 2023 at 09:29:38 UTC+1 Mark wrote:

> I have a package which has a function `Do[T comparable](a, b []T) 
> Result`.
> I have a struct:
> ```go
> type N struct {
>   x int
>   y int
>   t string
> }
> ```
> Is it possible to make `N` comparable; in particular by a field of 
> my choice, e.g., `t`?
>
> Or will I have to make, say, `DoFunc(a, b []N, eq func(i, j N) 
> bool) Result` with, say,
> `func eq(i, j N) { return i.t == j.t }`?
>


-- 
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/eb4db5e8-598f-4ce9-bc6d-38f9c5ed6861n%40googlegroups.com.


[go-nuts] Re: Can a struct be made comparable?

2023-07-14 Thread &#x27;Mark&#x27; via golang-nuts
I finally worked out how to make my go-diff pkg 
 (v1.1.0) able to 
diff sequences of structs based on a key function.

On Friday, July 14, 2023 at 12:27:46 PM UTC+1 Mark wrote:

> Hi Brian,
> Ah, thank you, that helped a lot.
>
> On Friday, July 14, 2023 at 11:54:51 AM UTC+1 Brian Candler wrote:
>
>> The 'diff' package you showed identifies the changed object index by 
>> means of a "Path" attribute in the Changelog entry. If the top level object 
>> is a slice, then Atoi(change.Path[0]) is the index into the slice.
>>
>> On Friday, 14 July 2023 at 10:47:47 UTC+1 Mark wrote:
>>
>>> Hi Brian,
>>> Your code certainly identifies the different items.
>>> However, that's not a diff tool in the sense I mean.
>>> Unix diff and tools like it don't just say x[i] != y[i], they find the 
>>> longest common subsequences and in essence produce a series of edit 
>>> commands that would turn slice x into slice y.
>>> There are quite a few go diff tools that will do this, including my own 
>>>  based on 
>>> Python's difflib sequence matcher.
>>> What I want to do is find one that does this for slices of structs where 
>>> only one struct field is considered for comparison purposes.
>>>
>>> On Friday, July 14, 2023 at 10:00:34 AM UTC+1 Brian Candler wrote:
>>>
 I forgot you wanted generics:
 https://go.dev/play/p/PhGVjsWWTdB

 On Friday, 14 July 2023 at 09:47:21 UTC+1 Brian Candler wrote:

> You seem to be saying "if the S field is different then I want to 
> consider these two structs different, and get pointers to the two 
> structs. 
> If the S field is the same then I want to skip the pair entirely". Is 
> that 
> right?
>
> The required semantics are not entirely clear, but it sounds like a 
> handful of lines of code to implement - there's no point importing and 
> learning a third party library.
>
> On the assumption that all the elements to be compared are in 
> corresponding positions in a and b:
> https://go.dev/play/p/Y71sLUpftzR
>
> On Friday, 14 July 2023 at 09:11:35 UTC+1 Mark wrote:
>
>> In fact the diff pkg mentioned above does work but is of no use to me 
>> since for each change it gives back only the field(s) used, not the 
>> original structs (or pointers to them), so I can't see any way back to 
>> the 
>> original structs (or their slice indexes).
>>
>> On Friday, July 14, 2023 at 8:58:41 AM UTC+1 Mark wrote:
>>
>>> What I really want to do is to be able to diff slices of structs on 
>>> the basis of one single field.
>>> For example, given:
>>> ```
>>> type Item struct {
>>>   I int
>>>   S string
>>> }
>>> ```
>>> and given `a` and `b` are both of type`[]Item`, I want to diff these 
>>> slices based purely on the `S` field, ignoring the `I` field.
>>>
>>> This diff pkg  claims 
>>> to be able to do this (something I'm testing, so I don't know either 
>>> way 
>>> yet), but in any case, it is incredibly slow.
>>>
>>> On Friday, July 14, 2023 at 8:31:39 AM UTC+1 Peter Galbavy wrote:
>>>
 As a slight digression - I thought I was going mad, but 'slices' 
 and 'maps' are new :-) Only in 1.21 though...

 Well, there is a lot of boiler plate that maps.Keys() will get rid 
 of.

 On Thursday, 13 July 2023 at 10:06:01 UTC+1 Brian Candler wrote:

> Structs are already comparable, but all fields must be the same:
> https://go.dev/play/p/XwhSz4DEDwL
>
> I think your solution with function 'eq' is fine.  You can see the 
> same thing in the standard library in slices.CompactFunc and 
> slices.EqualFunc
> https://pkg.go.dev/slices#CompactFunc
> https://pkg.go.dev/slices#EqualFunc
>
> For the case of "ordered" rather than "comparable", have a look at 
> slices.BinarySearchFunc and related functions.
>
> On Thursday, 13 July 2023 at 09:29:38 UTC+1 Mark wrote:
>
>> I have a package which has a function `Do[T comparable](a, b []T) 
>> Result`.
>> I have a struct:
>> ```go
>> type N struct {
>>   x int
>>   y int
>>   t string
>> }
>> ```
>> Is it possible to make `N` comparable; in particular by a field 
>> of my choice, e.g., `t`?
>>
>> Or will I have to make, say, `DoFunc(a, b []N, eq func(i, j N) 
>> bool) Result` with, say,
>> `func eq(i, j N) { return i.t == j.t }`?
>>
>

-- 
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 

[go-nuts] Re: Error handling

2023-07-30 Thread &#x27;Mark&#x27; via golang-nuts
Given that this proposal is to reduce boilerplate, and assuming the 
semantic issues could be solved, it seems to me that the 'return' is 
redundant (i.e., could be implicit) and that 'orelse' could be done with 
the existing 'else' keyword, i.e.,

```
result, err := someCall() else rest, err
```
Anyway, I really do hope the long-winded error syntax gets solved somehow!

On Monday, July 31, 2023 at 5:41:49 AM UTC+1 DrGo wrote:

> func myFirstFunction() (string, err) {
>
>result, err := myFunction() orelse return rest, err
>
> }
>
> On Sunday, July 30, 2023 at 9:27:27 PM UTC-6 Marcello H wrote:
>
>> I think the current error handling is just fine.
>> For the extra typing, they invented keyboard snippets and such.
>>
>> But for this proposal, I would like to see how a return with multiple 
>> values would look to get a better understanding.
>> ```
>> // translate this in the proposed solution?
>> func myFirstFunction() (string, err) {
>>result, err := myFunction()
>>if err != nill {
>>return rest, err
>>}
>> }
>> ```
>>
>> Op maandag 31 juli 2023 om 04:32:01 UTC+2 schreef DrGo:
>>
>>> Another possibility Jeremy is that the orelse block is executed if any 
>>> of the returned error values is not nil. 
>>>
>>> On Sunday, July 30, 2023 at 8:14:58 PM UTC-6 DrGo wrote:
>>>
 Thanks...
 yes indeed. Too many requirements but I think this solution comes close 
 to meeting them. If a rare function returns more than one error value (yet 
 to see one in the wild) then the compiler should reject orelse use and the 
 user can fallback on the (the if err!= nil) approach. 

 On Sunday, July 30, 2023 at 6:02:57 PM UTC-6 Jeremy French wrote:

> Also, errors are values, which means - although uncommon - a function 
> could return two or more error values.  Which would orelse evaluate?  
> Even 
> if you arbitrarily chose one, that would violate the explicit vs implicit 
> code flow principle.  
>
> My sympathies, OP.  I too hate the "if err!= nil" boilerplate, and 
> have suggested my own idea for fixing it, which was similarly dismantled 
> for good reasons by those more knowledgeable than me.  The truth is, this 
> problem/issue has so many restrictions placed on it (currently idiomatic 
> principles, backwards compatibility promise, explicit vs implicit, etc) 
> that the set of possible solutions is VERY narrow, possibly infinitely so.
>
> On Sunday, July 30, 2023 at 3:51:49 PM UTC-4 Brian Candler wrote:
>
> err := io.Copy(w, r) *orelse* {
> w.Close()
> os.Remove(dst)
> return fmt.Errorf("copy %s %s: %v", src, dst, err)
> }
>
> My question still stands. Semantically, what value exactly does the 
> "orelse" condition test is not equal to nil?
>
> - does it test the value from the preceding assignment? If so, is 
> "orelse" only valid immediately following an assignment expression? The 
> original posting didn't say this.  And if it *is* linked to an assignment 
> expression which assigns multiple values, does it only look at the last 
> value? (Again, that was not specified)
>
> - does it always test a variable called "err"? The original posting 
> said it was equivalent to "if err!=nil" but a later post contradicted this
>
> - does it test the value from the 'return' expression at the end of 
> the block following orelse? Except in this case, it can't because it's 
> buried inside fmt.Errorf
>
> On Sunday, 30 July 2023 at 17:55:34 UTC+1 DrGo wrote:
>
> Good point Harri,
>
> This is what the correct version will look like using this proposal 
>
> func CopyFile(src, dst string) error {
> r, err := os.Open(src) *orelse* return fmt.Errorf("copy %s %s: %v", 
> src, dst, err)
> defer r.Close()
>
> w, err := os.Create(dst); *orelse* return fmt.Errorf("copy %s %s: %v", 
> src, dst, err)
> err := io.Copy(w, r) *orelse* {
> w.Close()
> os.Remove(dst)
> return fmt.Errorf("copy %s %s: %v", src, dst, err)
> }
>
> err := w.Close() *orelse* {
> os.Remove(dst)
> return fmt.Errorf("copy %s %s: %v", src, dst, err)
> }
> }
>
> In a more complex func, the error formatting/handling code can be 
> further deduplicated by extracting it into a closure. 
> e.g., 
>
> func CopyFile(src, dst string) error {
> copyErr:= func(err error) {
> return fmt.Errorf("copy %s %s: %v", src, dst, err)
> } 
> r, err := os.Open(src) *orelse* return copyErr(err) 
> defer r.Close()
>
> w, err := os.Create(dst); *orelse* return copyErr(err)
> err := io.Copy(w, r) *orelse* {
> w.Close()
> os.Remove(dst)
> return copyErr(err)
> }
>
> err := w.Close() *orelse* {
> os.Remove(dst)
> return copyErr(err)
> }
> }
>
> On Sunday, July 30, 2023 at 8:17:31 AM UTC-6 Harri L wrote:
>>>

[go-nuts] How to convert an svg to a png (or gif) image?

2023-08-03 Thread &#x27;Mark&#x27; via golang-nuts
I know this has been asked before, just wondered if there were any pure-Go 
solutions?

-- 
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/e8a39bee-742a-4491-809a-b2230ef8ea26n%40googlegroups.com.


[go-nuts] Re: How to convert an svg to a png (or gif) image?

2023-08-04 Thread &#x27;Mark&#x27; via golang-nuts
Thanks!

On Friday, August 4, 2023 at 8:46:18 AM UTC+1 Tamás Gulácsi wrote:

> https://pkg.go.dev/github.com/goki/gi/svg
>
> Mark a következőt írta (2023. augusztus 3., csütörtök, 13:18:48 UTC+2):
>
>> I know this has been asked before, just wondered if there were any 
>> pure-Go solutions?
>>
>

-- 
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/6c0c3e50-72ab-42bb-9579-f8c9c967d5f5n%40googlegroups.com.


[go-nuts] How to constrain an integral type's values

2023-09-08 Thread &#x27;Mark&#x27; via golang-nuts
I often create small multi-value flag types, e.g.
```go
type mode uint8

const (
argMode mode = iota
baseMode
cmdMode
)
```
The problem is that if I write, say,  `m := baseMode`, although `m` has the 
correct type (`mode`), there is no way to constrain its values to the 
consts I've declared.
In theory I could create a `NewMode(int) mode` function and have it panic 
if the given int isn't valid; but that won't prevent, say, `m += 99`. The 
next step would be to define a full type, e.g., something like:
```go
type mode struct {
m int
}
```
This would prevent `m++` and similar since all accesses would have to go 
through the public functions. However, this would still do all its checking 
at _runtime_; whereas for this kind of use it should surely be possible to 
check at compile time.

In Pascal it is easy to declare a "subrange" type which might look like 
this in Go-like syntax:

`type mode uint8 0..2`

Is there a compile-time solution for this that I've missed?

-- 
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/8ba9ef87-984c-4b14-a760-343d2731e91dn%40googlegroups.com.


[go-nuts] bufio.Scanner - possible bug or doc err?

2023-10-12 Thread &#x27;Mark&#x27; via golang-nuts
I'm reading Debian *Package files, some of which are over 1M lines long.
I used bufio.Scanner and found that it won't read past 1M lines (I'm using 
Go 1.21.1 linux/amd64).
Is this a limitation of bufio.Scanner? If so then it ought to be in the 
docs.
Or is it a bug?
Or maybe I made a mistake (although using bufio.Scanner seems easy)?
```
scanner := bufio.NewScanner(file)
lino := 1
for scanner.Scan() {
line := scanner.Text()
lino++
... // etc
}
```
Anyway, I've switched to using bufio.Reader and that works great.

-- 
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/69f2fa03-c650-4c02-9470-51894dc56d1an%40googlegroups.com.


Re: [go-nuts] bufio.Scanner - possible bug or doc err?

2023-10-12 Thread &#x27;Mark&#x27; via golang-nuts
I have written and attached an example that compares bufio.Reader and 
bufio.Scanner.
Here's the output from `go run .` (a line count followed by the first error 
encountered):
```
Reader 1333665 
Scanner 58 bufio.Scanner: token too long
```
This probably _won't_ fail on your 2M line file; it looks like the problem 
is with the line length of a Debian Packages file. If you have a 
Debian-derived distro you could try replacing the filename in the file with 
one from `/var/lib/apt/lists/`.

The docs for bufio.Scanner do say
"Programs that need more control over error handling or large tokens, or 
must run sequential scans on a reader, should use bufio.Reader instead"
Perhaps it would be more helpful to mention what the token length limit is?

On Thursday, October 12, 2023 at 9:45:10 AM UTC+1 Rob Pike wrote:

> I just did a simple test with a 2M line file and it worked fine, so I 
> suspect it's a bug in your code. But if not, please provide a complete 
> working executable example, with data, to help identify the problem.
>
> -rob
>
>
> On Thu, Oct 12, 2023 at 7:39 PM 'Mark' via golang-nuts <
> golan...@googlegroups.com> wrote:
>
>> I'm reading Debian *Package files, some of which are over 1M lines long.
>> I used bufio.Scanner and found that it won't read past 1M lines (I'm 
>> using Go 1.21.1 linux/amd64).
>> Is this a limitation of bufio.Scanner? If so then it ought to be in the 
>> docs.
>> Or is it a bug?
>> Or maybe I made a mistake (although using bufio.Scanner seems easy)?
>> ```
>> scanner := bufio.NewScanner(file)
>> lino := 1
>> for scanner.Scan() {
>> line := scanner.Text()
>> lino++
>> ... // etc
>> }
>> ```
>> Anyway, I've switched to using bufio.Reader and that works great.
>>
>> -- 
>> 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...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/69f2fa03-c650-4c02-9470-51894dc56d1an%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/69f2fa03-c650-4c02-9470-51894dc56d1an%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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/ebf242ab-c8aa-4de8-821b-3abe77a9da86n%40googlegroups.com.
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

const pkgFile = "/var/lib/apt/lists/gb.archive.ubuntu.com_ubuntu_" +
	"dists_jammy_universe_binary-amd64_Packages"

func main() {
	lines, err := readPackages(pkgFile)
	fmt.Println("Reader", lines, err)
	lines, err = scanPackages(pkgFile)
	fmt.Println("Scanner", lines, err)
}

func readPackages(filename string) (int, error) {
	file, err := os.Open(filename)
	if err != nil {
		return 0, err
	}
	defer file.Close()
	reader := bufio.NewReader(file)
	lines := 0
	for {
		_, err := reader.ReadString('\n')
		if err == io.EOF {
			break
		} else if err != nil {
			return 0, err
		}
		lines++
	}
	return lines, nil
}

func scanPackages(filename string) (int, error) {
	file, err := os.Open(filename)
	if err != nil {
		return 0, err
	}
	defer file.Close()
	scanner := bufio.NewScanner(file)
	lines := 0
	for scanner.Scan() {
		_ = scanner.Text()
		lines++
	}
	return lines, scanner.Err()
}


Re: [go-nuts] bufio.Scanner - possible bug or doc err?

2023-10-12 Thread &#x27;Mark&#x27; via golang-nuts
Yes, I can see now.

Perhaps consider changing:

Programs that need more control over error handling or large tokens, or 
must run sequential scans on a reader, should use bufio.Reader instead. 

to:

Programs that need more control over error handling or large tokens (such 
as lines longer than MaxScanTokenSize), or must run sequential scans on a 
reader, should use bufio.Reader instead. 

Just a thought.

Thanks.
On Thursday, October 12, 2023 at 8:56:05 PM UTC+1 Ian Lance Taylor wrote:

> On Thu, Oct 12, 2023 at 10:21 AM 'Mark' via golang-nuts
>  wrote:
> >
> > The docs for bufio.Scanner do say
> > "Programs that need more control over error handling or large tokens, or 
> must run sequential scans on a reader, should use bufio.Reader instead"
> > Perhaps it would be more helpful to mention what the token length limit 
> is?
>
> It's MaxScanTokenSize: https://pkg.go.dev/bufio#pkg-constants . See
> also https://pkg.go.dev/bufio#Scanner.Buffer .
>
> Ian
>

-- 
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/0392f8b3-a006-4bc0-aa54-3759aa0d3b7en%40googlegroups.com.


Re: [go-nuts] bufio.Scanner - possible bug or doc err?

2023-10-14 Thread &#x27;Mark&#x27; via golang-nuts
Yes, that's a much better solution.

On Friday, October 13, 2023 at 8:40:45 PM UTC+1 Ian Lance Taylor wrote:

> On Thu, Oct 12, 2023 at 11:42 PM 'Mark' via golang-nuts
>  wrote:
> >
> > Yes, I can see now.
> >
> > Perhaps consider changing:
> >
> > Programs that need more control over error handling or large tokens, or 
> must run sequential scans on a reader, should use bufio.Reader instead.
> >
> > to:
> >
> > Programs that need more control over error handling or large tokens 
> (such as lines longer than MaxScanTokenSize), or must run sequential scans 
> on a reader, should use bufio.Reader instead.
>
> Thanks, instead of that I added a link to Scanner.Buffer
> (https://go.dev/cl/535216). I hope that will help guide people in the
> right direction.
>
> Ian
>
>
> > On Thursday, October 12, 2023 at 8:56:05 PM UTC+1 Ian Lance Taylor wrote:
> >>
> >> On Thu, Oct 12, 2023 at 10:21 AM 'Mark' via golang-nuts
> >>  wrote:
> >> >
> >> > The docs for bufio.Scanner do say
> >> > "Programs that need more control over error handling or large tokens, 
> or must run sequential scans on a reader, should use bufio.Reader instead"
> >> > Perhaps it would be more helpful to mention what the token length 
> limit is?
> >>
> >> It's MaxScanTokenSize: https://pkg.go.dev/bufio#pkg-constants . See
> >> also https://pkg.go.dev/bufio#Scanner.Buffer .
> >>
> >> Ian
> >
> > --
> > 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...@googlegroups.com.
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/0392f8b3-a006-4bc0-aa54-3759aa0d3b7en%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/a74e8c1b-e11e-4f5d-b66c-43bef18e1715n%40googlegroups.com.


[go-nuts] Is there a Go equivalent to Python's ctypes library?

2023-11-21 Thread &#x27;Mark&#x27; via golang-nuts
I would like to be able to access a dynamic C library (and ideally C++), 
i.e., `.so` or `.dll` _without_ using cgo, i.e., without needing a compiler 
on the target platform.
Python provides a `ctypes` module with this facility. Is there an 
equivalent for 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/f7f9a1e1-51d2-4144-9cec-d0dcaa31411en%40googlegroups.com.


[go-nuts] Re: Is there a Go equivalent to Python's ctypes library?

2023-11-21 Thread &#x27;Mark&#x27; via golang-nuts
Thank you, that looks just like what I want.

On Tuesday, November 21, 2023 at 10:21:14 AM UTC Mark wrote:

> I would like to be able to access a dynamic C library (and ideally C++), 
> i.e., `.so` or `.dll` _without_ using cgo, i.e., without needing a compiler 
> on the target platform.
> Python provides a `ctypes` module with this facility. Is there an 
> equivalent for 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/6e31d1e4-159e-4120-a76a-931df7a213f5n%40googlegroups.com.


[go-nuts] Re: Is there a Go equivalent to Python's ctypes library?

2023-11-21 Thread &#x27;Mark&#x27; via golang-nuts
My reply was to Salih but somehow my reply ended up appearing first.

According to the purego docs you don't need a compiler, essentially it 
provides a means of opening a dll/so and registering and calling functions 
within the dll/so.

On Tuesday, November 21, 2023 at 12:58:20 PM UTC Tamás Gulácsi wrote:

> Which is what?
> Please share with the golang-nuts list if you've found a solution!
>
> Mark a következőt írta (2023. november 21., kedd, 11:54:11 UTC+1):
>
>> Thank you, that looks just like what I want.
>>
>> On Tuesday, November 21, 2023 at 10:21:14 AM UTC Mark wrote:
>>
>>> I would like to be able to access a dynamic C library (and ideally C++), 
>>> i.e., `.so` or `.dll` _without_ using cgo, i.e., without needing a compiler 
>>> on the target platform.
>>> Python provides a `ctypes` module with this facility. Is there an 
>>> equivalent for 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/94a176f0-a076-4140-8179-5813ef061249n%40googlegroups.com.


[go-nuts] a simple linux audio player pkg?

2023-11-22 Thread &#x27;Mark&#x27; via golang-nuts
Is there a simple vorbis/oga audio player package for Go that works on 
Linux.
The only API I need is something like this:

player.SetFilename(string) error // string is say tune.ogg; stops playing 
previous if any
player.Play(float32) error // plays current filename from given second e.g. 
0.0 for the beginning
player.Pause() (float32, error) // pauses current playing and returns 
position
player.Resume() error // resumes playing
player.Secs() float32 // returns current position

The ones I've seen on awsome go either don't seem to play ogg format or are 
far more sophisticated than I need.

-- 
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/a38e3a9b-a357-4c85-aa4d-a7a4322ec216n%40googlegroups.com.


Re: [go-nuts] a simple linux audio player pkg?

2023-11-23 Thread &#x27;Mark&#x27; via golang-nuts
Thank you, I'll try them. (Well, for oto I'll try 
https://pkg.go.dev/github.com/hajimehoshi/oto/v2 since that seems to have 
more importers)

On Wednesday, November 22, 2023 at 10:31:25 PM UTC Raffaele Sena wrote:

> I have used github.com/jfreymuth/oggvorbis to read the ogg file (and 
> convert to PCM) and github.com/ebitengine/oto/v3 to play the PCM.
> I don't know of a full ogg player in Go
>
>
> On Wed, Nov 22, 2023 at 2:02 PM 'Mark' via golang-nuts <
> golan...@googlegroups.com> wrote:
>
>> Is there a simple vorbis/oga audio player package for Go that works on 
>> Linux.
>> The only API I need is something like this:
>>
>> player.SetFilename(string) error // string is say tune.ogg; stops playing 
>> previous if any
>> player.Play(float32) error // plays current filename from given second 
>> e.g. 0.0 for the beginning
>> player.Pause() (float32, error) // pauses current playing and returns 
>> position
>> player.Resume() error // resumes playing
>> player.Secs() float32 // returns current position
>>
>> The ones I've seen on awsome go either don't seem to play ogg format or 
>> are far more sophisticated than I need.
>>
>> -- 
>> 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...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/a38e3a9b-a357-4c85-aa4d-a7a4322ec216n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/a38e3a9b-a357-4c85-aa4d-a7a4322ec216n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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/85e7b5c4-7224-4f79-accd-ef1b4f095425n%40googlegroups.com.


Re: [go-nuts] a simple linux audio player pkg?

2023-11-23 Thread &#x27;Mark&#x27; via golang-nuts
I've now tried using those libraries, but there seems to be an 
incompatibility []float32 vs []byte.
Here's the error:

./play.go:24:29: cannot use reader (variable of type *oggvorbis.Reader) as 
io.Reader value in argument to otoCtx.NewPlayer: *oggvorbis.Reader does not 
implement io.Reader (wrong type for method Read)
have Read([]float32) (int, error)
want Read([]byte) (int, error)

And here's the code (main.go):

package main

import (
"log"
"os"
"time"

//"github.com/ebitengine/oto/v3"
"github.com/hajimehoshi/oto/v2"
"github.com/jfreymuth/oggvorbis"
)

func main() {
log.SetFlags(0)
file, err := os.Open(os.Args[0])
checkErr(err)
defer file.Close()
reader, err := oggvorbis.NewReader(file)
checkErr(err)
otoCtx, readyCh, err := oto.NewContext(reader.SampleRate(),
reader.Channels(), 2)
checkErr(err)
<-readyCh // wait for h/w
player := otoCtx.NewPlayer(reader)
defer player.Close()
player.Play()
for player.IsPlaying() {
time.Sleep(time.Millisecond * 10)
}
}

func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}

On Wednesday, November 22, 2023 at 10:31:25 PM UTC Raffaele Sena wrote:

> I have used github.com/jfreymuth/oggvorbis to read the ogg file (and 
> convert to PCM) and github.com/ebitengine/oto/v3 to play the PCM.
> I don't know of a full ogg player in Go
>
>
> On Wed, Nov 22, 2023 at 2:02 PM 'Mark' via golang-nuts <
> golan...@googlegroups.com> wrote:
>
>> Is there a simple vorbis/oga audio player package for Go that works on 
>> Linux.
>> The only API I need is something like this:
>>
>> player.SetFilename(string) error // string is say tune.ogg; stops playing 
>> previous if any
>> player.Play(float32) error // plays current filename from given second 
>> e.g. 0.0 for the beginning
>> player.Pause() (float32, error) // pauses current playing and returns 
>> position
>> player.Resume() error // resumes playing
>> player.Secs() float32 // returns current position
>>
>> The ones I've seen on awsome go either don't seem to play ogg format or 
>> are far more sophisticated than I need.
>>
>> -- 
>> 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...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/a38e3a9b-a357-4c85-aa4d-a7a4322ec216n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/a38e3a9b-a357-4c85-aa4d-a7a4322ec216n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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/b719492d-bead-4391-8371-2a6489f9cf46n%40googlegroups.com.


Re: [go-nuts] a simple linux audio player pkg?

2023-11-23 Thread &#x27;Mark&#x27; via golang-nuts
I just converted the []float32 to []byte (see function below) and it works.
But the sound produced while recognizable is very staticy and I don't know 
why.

func floats32ToBytes(fs []float32) []byte {
var buf bytes.Buffer
for _, f := range fs {
if err := binary.Write(&buf, binary.LittleEndian, f); err != nil {
panic(err)
}
}
return buf.Bytes()
}

On Thursday, November 23, 2023 at 10:03:24 AM UTC Mark wrote:

> I've now tried using those libraries, but there seems to be an 
> incompatibility []float32 vs []byte.
> Here's the error:
>
> ./play.go:24:29: cannot use reader (variable of type *oggvorbis.Reader) as 
> io.Reader value in argument to otoCtx.NewPlayer: *oggvorbis.Reader does not 
> implement io.Reader (wrong type for method Read)
> have Read([]float32) (int, error)
> want Read([]byte) (int, error)
>
> And here's the code (main.go):
>
> package main
>
> import (
> "log"
> "os"
> "time"
>
> //"github.com/ebitengine/oto/v3"
> "github.com/hajimehoshi/oto/v2"
> "github.com/jfreymuth/oggvorbis"
> )
>
> func main() {
> log.SetFlags(0)
> file, err := os.Open(os.Args[0])
> checkErr(err)
> defer file.Close()
> reader, err := oggvorbis.NewReader(file)
> checkErr(err)
> otoCtx, readyCh, err := oto.NewContext(reader.SampleRate(),
> reader.Channels(), 2)
> checkErr(err)
> <-readyCh // wait for h/w
> player := otoCtx.NewPlayer(reader)
> defer player.Close()
> player.Play()
> for player.IsPlaying() {
> time.Sleep(time.Millisecond * 10)
> }
> }
>
> func checkErr(err error) {
> if err != nil {
> log.Fatal(err)
> }
> }
>
> On Wednesday, November 22, 2023 at 10:31:25 PM UTC Raffaele Sena wrote:
>
>> I have used github.com/jfreymuth/oggvorbis to read the ogg file (and 
>> convert to PCM) and github.com/ebitengine/oto/v3 to play the PCM.
>> I don't know of a full ogg player in Go
>>
>>
>> On Wed, Nov 22, 2023 at 2:02 PM 'Mark' via golang-nuts <
>> golan...@googlegroups.com> wrote:
>>
>>> Is there a simple vorbis/oga audio player package for Go that works on 
>>> Linux.
>>> The only API I need is something like this:
>>>
>>> player.SetFilename(string) error // string is say tune.ogg; stops 
>>> playing previous if any
>>> player.Play(float32) error // plays current filename from given second 
>>> e.g. 0.0 for the beginning
>>> player.Pause() (float32, error) // pauses current playing and returns 
>>> position
>>> player.Resume() error // resumes playing
>>> player.Secs() float32 // returns current position
>>>
>>> The ones I've seen on awsome go either don't seem to play ogg format or 
>>> are far more sophisticated than I need.
>>>
>>> -- 
>>> 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...@googlegroups.com.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/golang-nuts/a38e3a9b-a357-4c85-aa4d-a7a4322ec216n%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/golang-nuts/a38e3a9b-a357-4c85-aa4d-a7a4322ec216n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>

-- 
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/4bbdeb78-6ac8-49c3-b71f-a270bb090a38n%40googlegroups.com.


[go-nuts] range func experiment typo?

2024-01-19 Thread &#x27;Mark&#x27; via golang-nuts
In the RangefuncExperiment  doc it 
says:

for k, v := range g { ... } // g has type Seq[K,V], k and v have types K 
and V

but shouldn't this be:

for k, v := range g { ... } // g has type Seq2[K,V], k and v have types K 
and V

Anyway, it looks interesting.

-- 
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/d5299dee-cabc-40a9-9c93-b9fd17149045n%40googlegroups.com.


[go-nuts] Can't figure out how to create an All() iter.Seq[T] method

2024-03-13 Thread &#x27;Mark&#x27; via golang-nuts
I've been trying to learn how to create an `All() iter.Seq[T]` method for a 
simple set type (a map wrapper):

playground 

It won't work in the playground of course, but when I do:
`GOEXPERIMENT=rangefunc go run .`
using Go 1.22 I get this error:
```
cannot use func(yield func(int, T) bool) {…} (value of type func(yield 
func(int, T) bool)) as iter.Seq[T] value in return statement
```
Can someone help me get this right please?

```
package main

import (
"cmp"
"fmt"
"iter"
)

func main() {
s := New(5, 10, 15, 20, 35)
for v := range s.All() {
fmt.Println(v)
}
}

type Set[T cmp.Ordered] map[T]struct{}

func New[T cmp.Ordered](elements ...T) Set[T] {
set := make(Set[T], len(elements))
for _, element := range elements {
set[element] = struct{}{}
}
return set
}

func (me Set[T]) All() iter.Seq[T] {
return func(yield func(int, T) bool) { // ERROR
n := 0
for key := range me {
if !yield(n, key) {
return
}
n++
}
}
}
```

-- 
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/73161d74-12eb-4be0-9ea4-a35a25758e8en%40googlegroups.com.


Re: [go-nuts] Can't figure out how to create an All() iter.Seq[T] method

2024-03-13 Thread &#x27;Mark&#x27; via golang-nuts
Thank you, that solved the problem.

On Wednesday, March 13, 2024 at 1:22:06 PM UTC Sebastien Binet wrote:

> hi Mark,
>
> On Wed Mar 13, 2024 at 13:32 CET, 'Mark' via golang-nuts wrote:
> > I've been trying to learn how to create an `All() iter.Seq[T]` method
> > for a
> > simple set type (a map wrapper):
> >
> > playground <https://go.dev/play/p/wtWvvTf33AF?v=gotip>
> >
> > It won't work in the playground of course, but when I do:
> > `GOEXPERIMENT=rangefunc go run .`
> > using Go 1.22 I get this error:
> > ```
> > cannot use func(yield func(int, T) bool) {…} (value of type func(yield
> > func(int, T) bool)) as iter.Seq[T] value in return statement
> > ```
> > Can someone help me get this right please?
>
> IIRC,
>
> ```
> return func(yield func(int, T) bool) { ... }
> ```
> is for iter.Seq2[int,T]
>
> if you want iter.Seq[T] you should write:
>
> ```
> return func (yield func(T) bool) { ... }
> ```
> (and adapt accordingly)
>
> hth,
> -s
>

-- 
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/063c64c8-ebfe-4039-abce-4b405e74d3e8n%40googlegroups.com.