Skip to content

Commit cf973aa

Browse files
committed
push some video ideas
1 parent 5c203ce commit cf973aa

2 files changed

Lines changed: 173 additions & 0 deletions

File tree

presentations/todo/auto_go.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Someone asked me how did I do this $CLIP in one of my recent videos.
2+
I'm going to walk you through how easy it is to do and to show you
3+
that on the fly scripting for nvim can give you lots of cool results!
4+
5+
```lua
6+
7+
local bufnr = 0
8+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { "line number 1", "the next line" })
9+
```
10+
11+
then show how to run something
12+
13+
```lua
14+
-- I use plenary, cause it makes things easier. If you have telescope, then you've installed plenary
15+
local Job = require "plenary.job"
16+
17+
Job:start {
18+
"go", "run", "main.go",
19+
on_exit = function(...)
20+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { "main.go output:" })
21+
vim.api.nvim_buf_set_lines(bufnr, -1, -1, false, output)
22+
end,
23+
}
24+
```
25+
26+
then just source that file. you'll see it run.
27+
28+
Now, you just need an autocmd
29+
30+
31+
```lua
32+
33+
local Job = require "plenary.job"
34+
35+
local group = vim.api.nvim_create_augroup(...)
36+
vim.api.nvim_create_autocmd("BufWritePost", {
37+
group = group,
38+
buffer = bufnr,
39+
callback = function()
40+
Job:start {
41+
"go", "run", "main.go",
42+
on_exit = function(...)
43+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { "main.go output:" })
44+
vim.api.nvim_buf_set_lines(bufnr, -1, -1, false, output)
45+
end,
46+
}
47+
end,
48+
})
49+
50+
```
51+
52+
Run this and you're all set. You can do the same with a test runner, set it to only do that for certain file paths,
53+
wahtever. It's all up to you and your own... personalized development environment!
54+
55+
56+
And remember, if you're doing this often, that means that you could just save this as a function for yourself
57+
later and then easily do this for different patterns, for input patterns (via vim.fn.input for example) and more!
58+
Don't forget that Lua is a programming language, not just a configuration setup for nvim :)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
2+
# Clarifications
3+
4+
- Don't think they should remove the feature in Go 1
5+
- However, there is talk of removing it for Go 2 (which is a thing?)
6+
https://github.com/golang/go/issues/21291
7+
8+
SO IM DEFINITELY NOT CRAZY
9+
10+
- I just wanted to make tons of Go puns
11+
- please rewatch the video if you didn't notice them. That means my delivery was perfect dad tier
12+
13+
- I was just saying that the feature *should not have been made* not that it should be deleted. Those
14+
are two very different things when you're talking about an existing language, feature, product, etc.
15+
16+
- Named Return vs. Naked Return
17+
- Named Return Values
18+
- So let's start with making it clear that I'm not opposed to this feature in general.
19+
- I think it's nice that you can "self-document" the code with naming the return values
20+
and that can show up in documentation
21+
22+
- If naked returns did not exist, I would liked named returns better!
23+
24+
- However, just because I like the feature doesn't mean that I'm confident in its worthiness of existing in Go.
25+
- is it really so much better than typing `var x, y int` at the top of your function?
26+
- They don't behave like other variables. Someone mentioned in the comments that they are initialized
27+
and returned, but I actually gave an example where they were never used and still didn't generate
28+
a warning or compiler error (which I think they should, to be consistent).
29+
- But in that case, it's probably super annoying to use and if you want to ignore some parameters,
30+
that doesn't work out super great.
31+
32+
- Naked Return
33+
- I think I found out something even MORE horrible... defer functions
34+
35+
```go
36+
func ReturnId() (id int, err error) {
37+
defer func() {
38+
if id == 10 {
39+
err = fmt.Errorf("Invalid Id\n")
40+
}
41+
}()
42+
43+
id = 10
44+
45+
return
46+
}
47+
48+
func ReturnId2() (id int, err error) {
49+
defer func(id int) {
50+
if id == 10 {
51+
err = fmt.Errorf("Invalid Id\n")
52+
}
53+
}(id)
54+
55+
id = 10
56+
57+
return
58+
}
59+
60+
// RETURNS 2 ???
61+
// As one would expect. The deferred code runs after the normal code in the
62+
// function has ended, and before the calling code has resumed. Named return
63+
// values are in scope and can be read and modified.
64+
func c() (i int) {
65+
defer func() { i++ }()
66+
return 1
67+
}
68+
69+
// RETURNS ERROR????
70+
func test() (res int, err error) {
71+
res = 5
72+
defer func() { err = fmt.Errorf("error in defer") }()
73+
return res, nil
74+
}
75+
76+
77+
78+
// makes sense when you see it here but there's no way most people know this
79+
80+
package main
81+
82+
// Emonstrate that defered functions can chnage the return value
83+
// and one cannot simulate that with local variables.
84+
85+
import (
86+
"fmt"
87+
)
88+
89+
func incrementer(i *int) {
90+
*i++
91+
}
92+
93+
func test1() (i int) {
94+
defer incrementer(&i)
95+
96+
// Go copies 1 into the location named by "i" and then
97+
// calls defered functions.
98+
return 1
99+
}
100+
101+
func test2() int {
102+
i := 0
103+
defer incrementer(&i)
104+
105+
// The value named by "i" is copied first into the
106+
// return location and then the defered code is called.
107+
// Thus changes to i have no effect on the return value.
108+
return i
109+
}
110+
111+
func main() {
112+
fmt.Println("named result:", test1())
113+
fmt.Println("local variable:", test2())
114+
}
115+
```

0 commit comments

Comments
 (0)