[go-nuts] Re: Constrain a type to be either a slice or a map

2022-03-18 Thread Henry
I don't think there is such support in Go generics as of now. I may be wrong though. The alternative is to use reflection. Here is the simplified code of your example: ```go func GetValidator(field Field) Validator { return Validator{ Validate: func() (int, error) {

[go-nuts] Re: When will the official encoding/json package support parsing json5?

2022-03-18 Thread ben...@gmail.com
Yeah, Go's encoding/json will almost certainly never support json5. However, one very simple approach: if you're using JSON for a config file and just need ability to add // line comments, you can just write a simple transformer which reads the file line by line and filters out lines that

Re: [go-nuts] Re: Parsing a time as a prefix of a larger string

2022-03-18 Thread ben...@gmail.com
Thanks for the feedback, folks. I think we'll end up using an explicit "log-trim" regex for this. -Ben On Thursday, March 17, 2022 at 3:03:08 PM UTC+13 Rob 'Commander' Pike wrote: > I would approach the problem a different way and ask the question, how > do I split the string to separate the

Re: [go-nuts] how to protect source code

2022-03-18 Thread Holloway Kean Ho
>I want to encrypt my algorithm library, is there any good way in Go. My library is used in both Windows and Linux I'm assuming you're doing it for scalable distribution since Zhaoxun already answered local development environment. In this case it makes no sense as I can still use disassembler

[go-nuts] Mathematical operations - the generics way

2022-03-18 Thread Endre Simo
Now that generics are officially supported, I was checking in the Go source if there are some generic implementation of utility methods like Abs, Min, Max etc, but I couldn't find it other than this proposal https://github.com/golang/go/discussions/48287. Anyone knows if something related has

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread Marvin Renich
[Sorry, I accidentally set the Reply-To incorrectly in my previous msg; corrected here. I don't need a CC in responses.] * Jan Mercl <0xj...@gmail.com> [220318 12:04]: > On Fri, Mar 18, 2022 at 4:53 PM 'Axel Wagner' via golang-nuts > wrote: > > I don't really know the answer to your question. I

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread Jan Mercl
On Fri, Mar 18, 2022 at 4:53 PM 'Axel Wagner' via golang-nuts wrote: > > One thing to keep in mind is that Go development was incremental and was > done, from early on, by having multiple implementations. Incremental > processes often lead to different results than designing a language from a

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread 'Axel Wagner' via golang-nuts
One thing to keep in mind is that Go development was incremental and was done, from early on, by having multiple implementations. Incremental processes often lead to different results than designing a language from a blank slate. Ambiguities in the spec are often discovered only when someone

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread Marvin Renich
* 'Axel Wagner' via golang-nuts [220318 08:04]: > https://go.dev/ref/spec#Length_and_capacity says: > > > The expression len(s) is constant if s is a string constant. The > > expressions len(s) and cap(s) are constants if the type of s is an array or > > pointer to an array and the expression s

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread Jochen Voss
Thanks Axel, Now this makes sense to me. All the best, Jochen On Friday, 18 March 2022 at 12:04:04 UTC axel.wa...@googlemail.com wrote: > https://go.dev/ref/spec#Length_and_capacity says: > >> The expression len(s) is constant if s is a string constant. The >> expressions len(s) and cap(s)

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread 'Axel Wagner' via golang-nuts
https://go.dev/ref/spec#Length_and_capacity says: > The expression len(s) is constant if s is a string constant. The > expressions len(s) and cap(s) are constants if the type of s is an array or > pointer to an array and the expression s does not contain channel receives > or (non-constant)

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread 'Dan Kortschak' via golang-nuts
On Fri, 2022-03-18 at 04:43 -0700, Jochen Voss wrote: > Dear all, > > The spec at https://go.dev/ref/spec#For_range says > > "The range expression x is evaluated once before beginning the loop, > with one exception: if at most one iteration variable is present > and len(x) is constant, the range

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread Jan Mercl
On Fri, Mar 18, 2022 at 12:43 PM Jochen Voss wrote: > The spec at https://go.dev/ref/spec#For_range says > > "The range expression x is evaluated once before beginning the loop, with one > exception: if at most one iteration variable is present and len(x) is > constant, the range expression is

[go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread Jochen Voss
Dear all, The spec at https://go.dev/ref/spec#For_range says "The range expression x is evaluated once before beginning the loop, with one exception: if at most one iteration variable is present and len(x) is constant, the range expression is not evaluated." What does the second half of this

[go-nuts] Re: Constrain a type to be either a slice or a map

2022-03-18 Thread RussellLuo
Thanks for your reply, Henry! I admit that my problem seems a little bit silly without providing the contextual information. Actually I am trying to rewrite my little [validation library](https://github.com/RussellLuo/validating) by leveraging Go generics. For the originally non-generic

Re: [go-nuts] Constrain a type to be either a slice or a map

2022-03-18 Thread RussellLuo
Thanks, kortschak! I still need some time to understand the proposal in https://github.com/golang/go/issues/51338, but I think the code provided in the Go Playground is exactly what I want! On Friday, March 18, 2022 at 4:13:22 PM UTC+8 kortschak wrote: > On Thu, 2022-03-17 at 18:47 -0700,

Re: [go-nuts] Constrain a type to be either a slice or a map

2022-03-18 Thread 'Dan Kortschak' via golang-nuts
On Thu, 2022-03-17 at 18:47 -0700, RussellLuo wrote: > Is there any way to write a constraint, say, SliceOrMap, to support > either a slice or a map? > > With the help of SliceOrMap, then I can write a more generic version > `LenBetween` like this: > > ```go > func MapLenBetween[T SliceOrMap](s T,

[go-nuts] Re: Constrain a type to be either a slice or a map

2022-03-18 Thread Henry
Have you considered this? ```go func IsBetween(value, min, max int) bool { return value>=min && value <=max } if IsBetween(len(myMap), 10, 25) { //do something } ``` On Friday, March 18, 2022 at 11:20:37 AM UTC+7 RussellLuo wrote: > Hi there, > > Thanks to Go generics in 1.18, I can write