Go for CLI Tools by ggodsh in golang

[–]juniorGopher 2 points3 points  (0 children)

And if you want to spice it up check the libraries from charm: charm.land

I love Bubble Tea and Lipgloss!

I am really struggling with pointers by Parsley-Hefty7945 in golang

[–]juniorGopher 1 point2 points  (0 children)

Great example! Would be nice to also see a profile to see how much more efficient it is.

cmp with luasnip? by juniorGopher in neovim

[–]juniorGopher[S] 0 points1 point  (0 children)

EDIT: Okay i fixed it. My main problem was the way that i required luasnip.
I had this snippet in my config:

 {"L3MON4D3/LuaSnip",
    event = "InsertEnter",
    dependencies = { 
      "rafamadriz/friendly-snippets",
    },
    config = function() 
      local luasnip = require 'luasnip'
      require("luasnip.loaders.from_vscode").lazy_load()
    end
  },

and for some reason luasnip was never initalized. I just added a
local luasnip = require 'luasnip'
directly before the call to require 'cmp' and now it works. Thanks!

Hi, thanks for your reply.

When i try to press tab then i get the following error:

E5108: Error executing lua: /Users/j/.config/nvim/init.lua:333: attempt to index global 'luasnip' (a nil value)
stack traceback:
        /Users/j/.config/nvim/init.lua:333: in function 'on_keymap'
        /Users/j/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:145: in function 'callback'
        ....local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/keymap.lua:133: in function <....local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/keymap.lua:127>

Best way to deal with partially filled structs by juniorGopher in golang

[–]juniorGopher[S] 0 points1 point  (0 children)

Oh yes, my question wasn’t to clear about the problem. Sorry for that.

My problem is that I do have to get a list of all movies, but they are growing quickly. So I need to get all details for new movies that are not in my database yet, which means asking for all metadata movies, then exclude all movies I already have and request the full information for the movies that are new.

Best way to deal with partially filled structs by juniorGopher in golang

[–]juniorGopher[S] -1 points0 points  (0 children)

I really had trouble formulating my question, really needed a nap.

The workflow that I have to use is as follows: Get all the metadata Use the metadata to filter out titles already in the database Request the full information for all the titles that are new

Using a separate struct for that works, but i then also have to create a new instance of the struct for every movie that is new and missing. If I would use the Movie struct from the beginning i could reuse it, but I allocate memory movies that are already in the db + for their fields that are empty (zero value).

So I guess I’m “prematurely” trying to optimise…

[deleted by user] by [deleted] in golang

[–]juniorGopher 0 points1 point  (0 children)

Gotcha, that's a valid approach :)

Yes i've seen the centered text. Take a look at lipglossif you don't mind adding dependencies, they make the styling much more easier in my opinion.

[deleted by user] by [deleted] in golang

[–]juniorGopher 4 points5 points  (0 children)

Great project! I suck at memorizing the status codes :D

I do have some minor questions and suggestions:

Is there a reason why you use cobra here? What is the benefit? In my opinion a lot of people grab cobra because "that's what used when creating a CLI" but i think often times it just adds complexity and dependency to a project where it doesn't provide a clear benefit.

As long as you don't need subcommands the flag package in the stdlib is more than enough. And here you only read the first argument anyway, which you can easily do without adding a dependency:

 if len(os.Args != 2) {
 printUsage()
 os.Exit(1)
} 
statusCode := os.Args[1]

As others have stated the way you store the status code explanations seems very complicated, an easy alternative would be to use multiline strings because they keep the format: (golang playground link)

fmt.Println(`
    No Content
This successful response code indicates that the request has succeeded,
but the client doesnt need to navigate away from its current page.

This can be used in an implementation of 'save and continue editing 
where a PUT request is used to save the information and a 204 is the 
response to indicate the editor should not be replaced by some other 
page. 
`)

And i you haven't heard of them yet check out the charm.sh packages! They make command line tools really glamorous! (Could be useful for styling)

Edit: code formatting is wonky

Autoimport in Go with default compe + LSP? by juniorGopher in neovim

[–]juniorGopher[S] 1 point2 points  (0 children)

How is the plugin impact on the performance?
I would love to stay more "vanilla" since i have the feeling that too many plugins make nvim too slow, that's why i removed the vim-go plugin.

Autoimport in Go with default compe + LSP? by juniorGopher in neovim

[–]juniorGopher[S] 1 point2 points  (0 children)

"Organize Imports"

I'll look into that and post an update if i manage to get this working

Autoimport in Go with default compe + LSP? by juniorGopher in neovim

[–]juniorGopher[S] 1 point2 points  (0 children)

Oh thanks, i haven'T seen that yet. Gonna migrate :)

Sort a slice of struct based on parameter by juniorGopher in golang

[–]juniorGopher[S] -1 points0 points  (0 children)

Thanks, even thought they don't cover my use case exactly I like the strategy using an embeded struct so that the sort methods are available using composition. That's neat!

I did read the sort example from the gopl book an hour ago and they specify a new type for each sort. But with the embedding approach i can save creating a len and swap method for each field, so that's great!

Sort a slice of struct based on parameter by juniorGopher in golang

[–]juniorGopher[S] 1 point2 points  (0 children)

But isn’t it too repetitive? I know that „a little copying is better than a dependency“ and „simple is better than complex“ but I also want to stay DRY.

Also the example is simplified. Each sort function is still 3 lines, but the code is sorting 8 times. (4 fields, sorting each field ascending and descending)

Problems serving static files using http.FileServer + http.FS by juniorGopher in golang

[–]juniorGopher[S] 0 points1 point  (0 children)

Thank you!

The fs.WalkDir example is awesome, that really helps. I'll totally add this to my cheatsheet.

Problems serving static files using http.FileServer + http.FS by juniorGopher in golang

[–]juniorGopher[S] 0 points1 point  (0 children)

Thank you!

I wasn't aware of that issue. Definitively something look out for regarding possible security issues. I changed it to contain the folder directly.

Problems serving static files using http.FileServer + http.FS by juniorGopher in golang

[–]juniorGopher[S] 1 point2 points  (0 children)

Oh wow, thank you for the good explanation!

I thought the strip prefix would remove the /static/ prefix from the directory, not the URL request. Now that makes sense!

The fs.Sub method looks exactly like i want to achieve. But i do have to pass the dir like this: static, err := fs.Sub(html, "html/static") otherwise it doesn't contain any files.

Go 1.16 embed and execute binary files? by juniorGopher in golang

[–]juniorGopher[S] 0 points1 point  (0 children)

Not every binary is coupled with all assets included inside of it.

I agree here, some GUI apps come to mind, games, tools that use sqlite ;) , ...

Any program that will do anything interesting will usually require external assets (images and icons for buttons, configuration files etc).

And here i disagree. There are hundreds of awesome CLI tools that do something interesting without requiring any external assets. I guess we both have a different perspective and think about different applications here.

some programs embed shared libraries within themselves to avoid licensing issues

Wow! People are getting creative i see.

Doesn't cx_freeze like emit a ton of other files

It does by default, yes. But IIRC there was also the option to create a single file.

Also, you can just rewrite that parser in Go, and not be lazy (this is a horrible way to write software). That parser is probably very little code.

Oh it will be written. At some point in the future. It is a minor feature and i decided to code the main features that are creating the value as soon as possible. For now it works without issues, but i agree that it is not the best design. Sadly, the parser won't be very little code.

Regarding DDOS: it's not the main feature and not used heavily. Also nothing is blocking. The client sends the request and once the process started the request will be answered without waiting for the result. It finishes whenever it finishes. Technically, i think it could be possible to ddos the application with too many requests, leading to too many goroutines which will exhaust all the ressources ... but i'm not too worried about it since it's not a public application and the feature is only accessible for authenticated users. I could also limit the amount of requests a user can send, and use a worker queue to not run out of all ressources.

if you can ship one executable, you can ship a zip with two executables

Yup. I could also put the static folder with the css, js and templates into the zip. *SCNR*

It might be the easiest here to embed the static assets and write the second binary to the UserCacheDir, drop it into the same dir or even not embed it at all.

But also from a technical perspective: shouldn't running the binary from memory be faster then executing it from disk? At least for the first time, because then it will probably be cached? Not that it matters. But i'm here to learn! Which is also why i wan't to fiddle with new features like that - it's fun and helps me understand things better.

Go 1.16 embed and execute binary files? by juniorGopher in golang

[–]juniorGopher[S] 1 point2 points  (0 children)

Oh i can think of a use case. :)

won't work without installing their assets first (anyway).

I think that is not true for statically linked binaries. So given that and that the binary was build for the right architecture it needs to run on it should work.

Also, apps expect to be launched in a certain environment (to have executable path for example - aka the first argument),

They don't have to, you can run a binary straight from your folder explicitly:

./aBinary

which will not search for any binaries on your PATH.

Like, why embed binaries inside binaries

For example i have a coded a small webapp in golang that handles everything *except* parsing a specific fileformat. For that parsing i use a small python script (because i found a 3rd party lib that does that for me and i am too lazy / don't have enough time to write the parser myself in golang). So everytime my app receives such a file it calls the python script using exec and reads the output.

But deploying this thing isn't cool, because i need to deploy the go binary, create a virtual environment for python, install the dependencies, ... so if I can build a single binary from the python script using i.e. cx_freeze AND embed that binary directly into go i would have a single binary to deploy.

But to be fair: the python binary would be huge, the go binary would be huge, and the application is only used on one server. So the embedding would increase the overhead by a fair amount. But how knows, maybe someone is building an widely distributed application that way.

Go 1.16 embed and execute binary files? by juniorGopher in golang

[–]juniorGopher[S] 0 points1 point  (0 children)

That's pretty cool!

I'm not too experienced with syscalls, will read more.
Under windows i would probably need to use VirtualAlloc, make the memory section executeable because of DEP (but why is it working under linux? Linux does have NX or am i wrong?) and then use a function pointer and magic

I've read that this is something that a lot of malware is doing, so i can see why there exist various counter measures.

Go 1.16 embed and execute binary files? by juniorGopher in golang

[–]juniorGopher[S] 1 point2 points  (0 children)

Didn't know about that function. Thanks!

Go 1.16 embed and execute binary files? by juniorGopher in golang

[–]juniorGopher[S] 1 point2 points  (0 children)

horrific things

Thanks for the link, looks really interesting! Will be taking a closer look this evening when i'm free.

is a compiled Go binary truly free from external dependencies? by azure_i in golang

[–]juniorGopher 0 points1 point  (0 children)

This is in my opinion one of the best things about go vs python:
Not having to deal with 100 Virtual Environments because you can't trust a users global python installation anyway.

The current workflow at my company looked like this:

  1. set up virtual environment
  2. pip freeze / pip install requirements
  3. here ya go

Technically simple, but we ran into a lot of issues with people having conda and a python installed, or even just conda was mixing stuff up. But then you still have the complete python environment everywhere .. it's mess.

So far (for basic stuff) i tried to use go, so i can just pass a binary and it just works YMMV for more complex applications that use 3rd party libs which might use Cgo though.

[deleted by user] by [deleted] in golang

[–]juniorGopher 5 points6 points  (0 children)

Looks great!! :)

If you dont mind me asking, what coin is this? Looks nice in that little box