Re: [go-nuts] Re: Is it expected that signal.NotifyContext() changes the execution thread?

2023-10-03 Thread 'wagner riffel' via golang-nuts

On Tue Oct 3, 2023 at 05:54 AM UTC, Kurtis Rader wrote:
> Thank you to Ian and TheDiveO. I don't understand why functions like
> gocv.io/x/gocv.NewWindow() have to run on the initial OS thread (at least
> on macOS).

It's common for C and C++ libraries to use TLS (thread local storage)
to attach data/state to each thread, one example is the global errno
variable used in C to signal errors, each thread read/write its own
variable even though in code they are wirting "the same" variable,
using libc from cgo and reading errno for failures would give wrong
results if the goroutine moved OS threads. It's unrelated which thread
it is and that's why it's not a default, you could start NewWindow at
some point that its goroutine is running in thread4, and if it's not
pinned to run in thread4 you have the same issue with the "initial
thread".

ps: Specific with graphics, I know OpenGL retains thread-local data,
which might explain why libraries that have this common ancestor needs
to LockOSThread, I'm not sure about Mac and Windows.

-w

--
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/dcbc6ba0-54c8-5942-eb4d-120ba8d65f03%40104d.net.


Re: [go-nuts] shadowing of types by variables

2023-01-16 Thread 'wagner riffel' via golang-nuts

On 1/13/23 07:20, Gorka Guardiola wrote:
According to the spec it seems like it is legal to shadow a type with a 
variable, even a builtin type.
Is there any specific rationale for this? I guess that it makes scoping 
checks easier and faster, but still.




I don't think there is any special rationale behind it, in Go builtin 
types are predefined identifiers, not keywords as usual in other 
languages, thus those follow rules of identifiers, not keywords. 
Although it feels astonishing that you can shadow even predefined 
constants like "true := false", in my experience this haven't shown to 
be an issue.


-w

--
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/ff5efae5-937e-32dd-608a-8d8ac8d0417b%40104d.net.


Re: [go-nuts] Newbie question

2023-01-10 Thread 'wagner riffel' via golang-nuts

On 1/10/23 11:42, Daniel Jankins wrote:

Hi,

Why can you update a value in a map struct?



Sort answer, because m["foo"] is not addressable, but you can have the 
desired behavior using a temp variable:


func f() {
m := make(map[string]struct{ i int })
x := m["foo"]
x.i = 42
m["foo"] = x
}

BR.
-- w

--
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/dba18fd2-55cc-148b-48b2-6ae8ba90aa29%40104d.net.


Re: [go-nuts] get function body comments using ast

2022-10-10 Thread 'wagner riffel' via golang-nuts

On 10/9/22 11:09, Dmitry Belyaev wrote:

For a function like below I'd get only *example // doc comment*,

// doc comment
func example() {
   // body comment   < looking to extract this comment
}

but I am looking also to extract *// body comment*.

Please advise on how to extract function name, doc comment AND body 
comment altogether. Thank you!




I don't think go/parser adds comments to functions body []Stmt list, 
though if go through all comments in ast.File and filter by those that 
are in range with function's block statement, maybe that's enough.


https://go.dev/play/p/S4Iz504K8Gm

-w


--
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/3758c4a4-bff7-3c15-91ac-8b5cb3d550d3%40104d.net.


Re: [go-nuts] For range loop variable passing by reference to go routine causes memory leak

2022-10-08 Thread 'wagner riffel' via golang-nuts

On 08/10/2022 22:56, davy zhang wrote:

Original post on stackoverflow:

https://stackoverflow.com/questions/73985794/for-range-loop-variable-passing-by-reference-to-go-routine-causes-memory-leak


Code for reproducing the problem:

https://go.dev/play/p/7Xzx1Aauzhh

go version go1.19 darwin/amd64

go tool pprof http://localhost:6060/debug/pprof/heap

func1 is leaking

https://i.stack.imgur.com/LTXHn.png 

Is there any reason for this leaking?


Thank you for any input



I don't think it's related with for-range loops, both should "leak"
the same, I bet that's tMap, currently in Go maps do not shrink, even
if you delete keys. One way to reduce memory usage of a long-living
map is to at some threshold copy over all its elements into a new map,
and set the old to map to this copy, then the old map, if not
referenced elsewhere, will eventually be gc'ed and cleaned up.

-w

--
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/d227d9f0-90e1-fb62-2925-9a4729036708%40104d.net.


Re: [go-nuts] Executing a tar command from within a Go program

2022-04-20 Thread 'wagner riffel' via golang-nuts
Also, the Output method only returns what was wrote to stdout, tar
argument parsing errors are probably in stderr, using CombinedOutput()
has better effect to debug.

-- 
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/CJF7P5OFW5I8.2YK5VGDWTSBVV%40pampas.


Re: [go-nuts] Executing a tar command from within a Go program

2022-04-20 Thread 'wagner riffel' via golang-nuts
On Wed Apr 20, 2022 at 6:16 PM CEST, Dean Schulze wrote:
> I need to execute this tar command
>
> *tar xzf dir1/dir2/somefile.tgz --directory=dir1/dir2/*
>

Did you considered using the packages "archive/tar" and
"compress/gzip" to achive this?

> *argStr := "xzf dir1/dir2/somefile.tgz --directory=dir1/dir2/"output, err
> := exec.Command("tar", argStr).Output()*
>

exec.Command arguments are variadic, each argument is one argv, thus I
think you meant exec.Command("tar", "xzf", "dir1/dir2/somefile.tgz",
"--directory=dir1/dir2/")

-w

-- 
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/CJF7II5MD0Y9.2Y2HYXCZ4QDSC%40pampas.


Re: [go-nuts] Packages for Accessing .a Archives?

2022-04-10 Thread 'wagner riffel' via golang-nuts
On Sun Apr 10, 2022 at 4:23 AM CEST, jlfo...@berkeley.edu wrote:
> Other than what's in the Go distribution, I haven't been able to find any
> packages for accessing .a archives. Is there anything else out there?
> Google wasn't helpful.
>
> Cordially,
> Jon Forrest
>

I found these using https://pkg.go.dev/search?q=ar

https://pkg.go.dev/github.com/mkrautz/goar
https://pkg.go.dev/github.com/blakesmith/ar
https://pkg.go.dev/github.com/laher/argo/ar

-w

-- 
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/CJ6WFYGNL9OV.1A2BXUCN1KFUJ%40pampas.


Re: [go-nuts] float exactness

2022-04-10 Thread 'wagner riffel' via golang-nuts
On Sat Apr 9, 2022 at 3:56 PM CEST, 'Jack Li' via golang-nuts wrote:
> Why literal operation is exact, variable is not?
>
> fmt.Println(0.1 + 0.2) // 0.3 exactly
> fmt.Println(x + y) // 0.30004
>

Both aren't exact because floats can't represent 0.3 exactly, they
differ because literals and constants expressions have arbitrary
precision (http://golang.org/ref/spec/#Constants), so "0.1" and "0.2"
in "0.1 + 0.2" are exact, then when the exact "0.3" result is
converted to a float64 it becomes the closest possible, You can check
this if you ask to print with more precision

x, y := 0.1, 0.2
fmt.Printf("%.17f...\n", 0.1+0.2) // 0.2...
fmt.Printf("%.17f...\n", x+y) // 0.30004...

https://go.dev/play/p/2qSfoCZGaD6

-w

-- 
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/CJ6VDO2DK4XB.3VDUG02EUZ58S%40pampas.


Re: [go-nuts] Pointer to a pointer

2022-03-10 Thread 'wagner riffel' via golang-nuts
On Thu Mar 10, 2022 at 12:41 PM CET, Manlio Perillo wrote:
> Interesting example, thanks.
>
> But how does `type self *self` works?  If I remember correctly, it is not
> discussed in the Language Specification and in The Go Programming Language
> book.
>

I don't think it's mentioned in the specification, my bet is that
unless your type requires inifnity amout of memory (eg: `type t struct
{t}`) or the type is an interface and break its rules, (eg: `type
iface interface{ iface }`) you can use self-reference.

> By the way, in C `typedef self *self` is invalid (assuming my code is
> written correctly).

You're correct, C doesn't allow self reference in type decl, more
interesting types beside pointers:
  `type fn func() fn`
  `type s []s`
  `type ch chan ch`
  `type m map[*m]m`

-w

-- 
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/CIG6Z0BDF5P1.1O1MIIQNEHEZY%40pampas.


Re: [go-nuts] Read N bytes from a TLS stream and block the caller while still respecting the deadline

2021-11-30 Thread 'wagner riffel' via golang-nuts
On Tue Nov 30, 2021 at 11:20 PM CET, LastName Almaember wrote:
> It's meant to first read a nine-byte header (8 bits of a type field
> followed by a 64 bit length). In theory, it should work fine, but it
> always immediately returns InputTooShort (right at the first check).

io.Readers returns up to len(p), not exactly len(p), you have to loop
over calling read until you have enough input, there's io.ReadAtLeast
and io.ReadFull helpers that does that for you.

> A bufio *might* work for this, however, I'm not confident that it will
> respect the deadline set on the connection.
>

bufio will respect your deadline, it doesn't skip errors from the
underlying reader, just returns them up. I suspect that it doesn't solve
the issue mentioned above tho.

BR.
-w

-- 
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/CG3S8W6WU9LX.1XINQC5EZN91Q%40pampas.


Re: [go-nuts] x/tools error while trying to install

2021-05-07 Thread 'wagner riffel' via golang-nuts
On Fri May 7, 2021 at 11:10 AM -03, Eric Garcia wrote:
> Was trying to install all the tools, and hit the following errors.
>
> go version 1.16.3
>
> ```
> go get -u golang.org/x/tools/...
>

I think with go1.16 and later the correct way of installing programs
is with go install, it builds clean for me.
$ go install golang.org/x/tools/cmd/...@latest

-wagner

-- 
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/CB79QAS9VCSF.3L7J5LRYUD4R8%40pampas.


Re: [go-nuts] Why fmt.Println(math.Sqrt2) gives ...0951 not ...0950?

2021-05-01 Thread 'wagner riffel' via golang-nuts
On Sat May 1, 2021 at 11:12 AM -03, Kamil Ziemian wrote:
> Can you guide me to some materials about const in Go? Or maybe I should
> finally read Go spec, which I avoid for a long time?
>

You shouldn't avoid to read the spec, it's not as complicated as others
languages, and it's a great source of answers to such questions, i think
this is a great material on Go constatns:
https://blog.golang.org/constants

-- 
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/CB244ROIRVC2.PS34B65JEIQ9%40pampas.


Re: [go-nuts] how to pass analyzer's flag with 'go vet -vettool'

2021-04-14 Thread 'wagner riffel' via golang-nuts
On Wed Apr 14, 2021 at 12:25 AM -03, Xiangdong Ji wrote:
> I tried to modify fieldalignment to turn its '-fix' option on by
> default, change worked as expected if running fieldalignment as a
> standalone utility, but no rewriting is applied when running it as a
> tool of 'go vet', could any expert here shed a light? Thanks.
>

In the last time I check this, I remember I saw that `go vet` calls
`vettol` with -flags argument and then tries to match with the command
line passed, but fieldalignment doesn't list -fix, that's why I said it
was broken. Hope this might help you:
https://github.com/golang/go/blob/e22478/src/cmd/go/internal/vet/vetflag.go#L67

-- 
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/CANHTHBOHYT7.2C1Y5NKICHA8%40pampas.


Re: [go-nuts] Why does Go ignore HTTP_PROXY and HTTPS_PROXY if the proxy address is localhost

2021-04-13 Thread 'wagner riffel' via golang-nuts
On Tue Apr 13, 2021 at 7:07 PM UTC, Orson Cart wrote:
> I'm perplexed :(
>

Did you tried the /etc/hosts? I think that would do it, if not,
something like this may do the trick:

if debug {
t := http.DefaultTransport.(*http.Transport)
t.Proxy = func(r *http.Request) (*url.URL, error) {
return url.Parse("http://localhost:;)
}
}

note: i did not tested, just shooting.

--wagner

-- 
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/CAMVU7SLM2E5.16SWWYLIRF8HD%40pampas.


Re: [go-nuts] Why does Go ignore HTTP_PROXY and HTTPS_PROXY if the proxy address is localhost

2021-04-13 Thread 'wagner riffel' via golang-nuts
On Tue Apr 13, 2021 at 2:14 PM -03, Orson Cart wrote:
> Can anyone explain the reasoning behind this? It rather interferes with
> debugging traffic using a local proxy like Fiddler.
>

My guess here it's for preventing the remote from tricking the proxy to
make request to internal services that couldn't be reached otherwise.

> I've seen suggestions to define an alternative hostname in /etc/hosts
> but as far as I can tell this doesn't work either.
>

The code you linked doesn't seen to try that hard to block loopback
requests, I do think that an /etc/hosts entry to 127.0.0.1 should
bypass the proxy, if not I don't see any other way around other than
implementing the RoundTripper yourself, which shouldn't be hard for
such simple use.

--wagner

-- 
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/CAMSV7PV770P.3NLGXICF3GS3S%40pampas.


Re: [go-nuts] how to pass analyzer's flag with 'go vet -vettool'

2021-04-12 Thread 'wagner riffel' via golang-nuts
On Mon Apr 12, 2021 at 10:23 AM -03, Xiangdong Ji wrote:
> Hi,
>
> I'm trying to run "go vet -vettool=$(which fieldalignment) ./..." for a
> large project, and wish to turn its '-fix' option on, wondering how to
> pass
> that option?

Hi Xiangdong, I found that surprising that even docs didn't mention
this anywhere, I also got confused by how go vet -vettool works but I
think I understood after reading the source code, after a -vettool is
specified, all flags that are not build flags should be sent to the
vettool, apparently those old tools like fieldalignemnt are broken
with the new "vet command line protocol" mentioned in the source code,
even some flags for those tools is broken, it's clear to me that
there's a bug going on here. Please also consider that possible that I
just think I understood the problem. :)

BR.

--wagner

-- 
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/CAM7VOPMQSL3.12A9D1RI2IO7S%40pampas.


Re: [go-nuts] Re: Give us const arrays please :)

2021-04-12 Thread 'wagner riffel' via golang-nuts
On Mon Apr 12, 2021 at 5:04 AM -03, Henry wrote:
> The example you gave is actually a map[rune]bool in Go, and not an
> array. I

It's a Go array in form of a composite literal, see
https://golang.org/ref/spec#Composite_literals

-- 
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/CALQL1TE71G0.1BJM6NT7DTW7W%40pampas.


Re: [go-nuts] package is not in GOROOT

2021-04-09 Thread 'wagner riffel' via golang-nuts
On Wed, 7 Apr 2021 20:11:22 -0400
rob  wrote:

> Would it be a bad idea to use my ~go/src as a module?
> 

I don't think so, I do have one global module like this for my own toys
and throwaways programs. Yes, it would need to adjust your import paths
according to the module, when I migrated to module it was easier
because everything was inside the same global folder, so I just needed
to init a module with that name.

>    go mod tidy --> do I need this?
> 

go mod tidy adds missing modules in go.mod/go.sum that you're importing
from go sources, and removes dangling ones (not found in your go
sources but in your go.mod/go.sum), it's wise to run it if you're
initializing a module from an existing code base.

--wagner

-- 
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/20210409120133.3a413a09%40pampas.


Re: [go-nuts] package is not in GOROOT

2021-04-07 Thread 'wagner riffel' via golang-nuts
On Tue, 6 Apr 2021 19:39:00 -0400
rob  wrote:

>  > This example is on Win10 using go 1.16.3
> 
> Now I've created a directory tree outside of ~/go/src.  I'm using ~
> to mean %userprofile%, as this is win10
> 
> ~/gcode/rpng/rpng.go
> 
> ~/gcode/tokenize/tokenize.go
> 
> ~/gcode/hpcalc2/hpcalc2.go
> 
> And I updated my import statement to be "gcode/hpcalc2", etc.
> 
> Now I can use
> 
>   go run gcode/rpng/rpng.go
> 
> And I set GOBIN=c:\Users\rob\gcode
> 
>      go install gcode/rpng/rpng.go
> 
> and it installs to GOBIN.
> 
> At least it's working for me mostly the way it was before.  I just
> had to abandon my ~/go directory
> 
> Thanks for answering
> 
> Rob
> 

Hi Rob, it's good that you got it working, but I feel you're struggling
with modules inferred from your past emails due a confusion between a
module namespace and the file system, your package import paths and go
commands are relative to a *module* and not a directory, your current
module namespace is detected similarly as a git repository is, that is,
traverse current working directory up until a go.mod is found (or .git
for git), and then if a module is found, you import packages or use the
go commands in the format of *module_name*/folder/..., not file system
paths, what go cares as a namespace is the module name, not
absolute/relative file system paths.

For example, suppose our current working directory is "~/gcode/" and it
has a go.mod named¹ "foo", note that the folder is named gcode but the
module is "foo", you would install the "rpng" package inside this
module namespace "foo" with:
$ go install foo/rpng

you can refer file path relatives alike as well:
$ go install ./rpng # OK
$ go install rpng # invalid, would fail with the same not found message

you can think as dot expanding to "foo", not the file system gcode
folder, to make clear that it's not about file system paths, changing
our current working directory to "tokenize", we still can mention "rpng"
relative to the module, not the file system:
$ cd ~/gcode/tokenize
$ go install foo/rpng
$ go install ../rpng # valid as well

I'm not sure if this helps you or causes even more confusions, if so
sorry.

BR.

--wagner

[1] the module name is usually a domain, but not necessarily, refer to
https://pkg.go.dev/golang.org/x/mod/module#CheckPath for detailed
description.

-- 
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/20210407112419.1e5e0132%40pampas.


Re: [go-nuts] string matching

2021-04-02 Thread 'wagner riffel' via golang-nuts
On Thu Apr 1, 2021 at 4:45 PM -03, Sharan Guhan wrote:
> r := regexp.MustCompile(`Core Count: [0-9]+`)
> match := r.FindAllStringSubmatch(cmd_output, -1)

As of the "efficiently" part, you should compile the regexp once
instead of each call to ParseFillCpuInfo, a common practice is to use
a package-level scoped variable but even better (for efficiency) would
be keep it with strings.Index approach, as such:

count := -1
const substr = "Core Count: "
if i := strings.Index(input, substr); i >= 0 {
countStart := i + len(substr)

const digits = "0123456789"
j := 0
for strings.ContainsRune(digits, rune(input[countStart+j])) {
j++
}
count, err := strconv.Atoi(input[countStart : countStart+j])
// handle err
}

You can benchmark both approaches using testing.B, have fun.

https://golang.org/pkg/testing/#B

-- 
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/CACXYA0GSJR6.UVYEID10NHU0%40pampas.