10 ms·
Porting 4.5K lines of C to Go
- assafmo 9y agoSounds very uneventful... A few porting bugs and language differences but not a lot. At least it sounds like a great way to learn about flexbox internals.
- pacaro 9y agoThis was my take too. Some reinforcement about the utility of tests. 4.5k is a relatively small amount of code (particularly in C or similar)
- tigershark 9y agoThis is what really struck me: "The API is still a bit awkward by Go standards. Too many methods, not enough direct access to variables" Does he really mean that proper go code has direct access to fields rather than encapsulating them???
- Scaevolus 9y agoYes. If you have a struct, getters/setters are somewhat pointless, since member visibility control is only package level (protected/public depending on variable name capitalization). For interfaces, you can only invoke methods, so getters/setters are required.
- vvanders 9y agoYup, getters/setters in POJOs drive me up the wall. So much simpler in languages that have proper const/mutability qualifiers.
- fleshweasel 9y agoso..not Go :P
- vvanders 9y agoOh yeah, definitely not :)
- krylon 9y ago> proper const/mutability qualifiers I really like Go, but on the const/mutability front, it does no better than C, unfortunately.
- kjksf 9y agoI would phrase it like that: most languages (C++/Java/.NET) have unhealthy obsession with creating unnecessary setters/getters, paying for it with increased compile time, code bloat, slower code. In Java/C# land refactoring tools even give one-click access to "create me some setters/getters". This is not to say that abstracting access with functions has no place, but if all they do is get/set the value, they are unnecessary.
- surye 9y agoC# and Python (for examples) don't use getters and setters, they use properties which provide the same interface as direct access variables when the need arises for more complexity, client code doesn't need to know about it. Languages without this feature needs to make everything getter/setter if they don't want to break the API when something needs additional logic.
- fancy_pantser 9y agoPython does have properties, which work via decorators @property (which decorates a getter) and @foo.setter.
- typon 9y agoHe literally just said that
- TazeTSchnitzel 9y agoAnd indeed Go code has its fair share of (otherwise) unnecessary getters because of this.
- taspeotis 9y ago> paying for it with increased compile time, code bloat, slower code. Auto-properties in C# are one line of code, same as fields, and the getter/setter methods should always be inlined which turn them into field accesses anyway. So I'm not sure how that would affect compile times or lead to slower code in anything but some pathological use case. Or DEBUG, but at that point all performance bets are off. (Technically Microsoft can get worse performance on properties vs. fields when using NGen - that's why they have TargetedPatchingOptOutAttribute - but that doesn't apply to regular, non-Framework assemblies.) As for code bloat `public int SomeValue { get; private set; } = 6;` encapsulates a publically read-only/privately mutable property with a default value quite concisely...
- coldtea 9y ago>Does he really mean that proper go code has direct access to fields rather than encapsulating them??? Yeah, so?
- valarauca1 9y agoEncapsulation isn't _that_ useful. In Go it has real runtime cost as unlike C/C++ (you have an optimizing compiler) [1] , and in Java/C# you have the JIT which will optimize out your method calls over enough time. A lot of FP languages are perfectly fine building Abstractions without getters/setters. But in OOP land people believe this is a requirement. 2/4 OOP _principles_ encapsulation and inheritance have aged like dirty sweat socks in a hot gym locker. [1] https://klaig.blogspot.be/2012/09/the-go-language-is-faster-than-computer.html https://klaig.blogspot.be/2012/09/the-go-language-is-faster-...
- burntsushi 9y agoTake a look at the `time.Time` struct. It has a couple fields. What are the trade offs of making those fields public? (I contend that encapsulation is supremely useful, and the time package is a good exemplar.) Note that Go's compiler does do function inlining, so it might be possible to encapsulate your data without runtime costs. With that said, you certainly don't have the flexibility that you have in C/C++/Rust. Of course I agree that blindly adding getters/setters everywhere is bad juju. But to say encapsulation isn't useful is a step too far. Instead of thinking in terms of getters/setters, I'd rather think in terms of invariants on your data. If you have invariants you want to protect, then encapsulation is one possible tool you can use to protect them.
- kartickv 9y agoHe didn't say encapsulation is not useful. He said it's not that useful. In other words, blindly encapsulating everything because that's supposed to be the right way is overkill. Unless you're building an API.
- burntsushi 9y ago> He didn't say encapsulation is not useful. He said it's not that useful. So look at my comment as a refinement on that idea?
- nine_k 9y ago
- to3m 9y ago> As far as I can tell you can’t step through tests in the debugger Rant time. What to do when you write a test framework. 1. When test fails, print everything possible: desired expression, desired value, actual exression, actual value, line and file of failing call. When printing line and file, make the format configurable, with the default being the prevailing standard for the system, so that people can get clickable links in their text editor or terminal with a minimum of hassle. About 90% of the time, this will give people everything they need in order to at least get started on fixing the failed test 2. Don't make it hard to use the debugger. The remaining 10% of the time, people will need to step through the code. Some measurable fraction of the other 90%, they'll also end up needing to do this, because it looked like it was something simple but actually it was more than that. So don't make this hard 3. See steps 1 and 2 This might sound obvious, but it clearly isn't, because I've used several test frameworks that make running in the debugger rocket science, and print literally nothing but your own message (if even that) when the test fails. Like, you do 'assert.equals(x,y)', and it doesn't even show you what the values of x and y are, let alone figure out that maybe "x!=y" would be an obvious thing to print. This may not sound like a big deal with my stupid little example, but after you've written several tens of these you will start to see my point.
- gouggoug 9y agoSlightly related: The lack of an easy to use debugger is a huge issue for me when working with go. Coming from PHP, I spend 90% of my time in the debugger running my code line by lines (with xdebug coupled to phpstorm). A debugger is especially useful when working with other's people code and large softwares. For example, contributing to large Go projects like Kubernetes can be daunting due to the very steep learning curve. This curve would be greatly reduced if we had a good reliable go debugger, which doesn't seem to be the case at the moment. (I'm aware of delve, but last time I looked, it was still fairly complicated to have it work).
- abledon 9y agoHow is the debug experience in elixir compared to go ?
- leastangle 9y ago> As far as I can tell you can’t step through tests in the debugger Maybe I am missing something, but that is not correct. You can debug tests the same way as you debug any other code. Even works with the mentioned/used Go extension for VS Code.
- kjksf 9y agoIt's quite possible that it's just my laziness in figuring out how to do it. All I know is that hitting 'F5' in VSCode builds the binary and runs it under the debugger. How would I debug the equivalent of `go test` in VSCode?
- devuo 9y agoI have no idea in VSCode, but with JetBrains Gogland, debugging tests is stupidly easy. Simply add the breakpoint where you want it, left click on the play button in the line of the test declaration, click debug in the contextual menu, and off you go.
- ice109 9y agotoo bad sometimes it doesn't resolve pointers correctly
- lukehoban 9y agoIn Go test files, there's now a codelens annotation above every Test function with "run test" and "debug test" commands. Just click "debug test" to launch a debugger against that test. See https://twitter.com/ramyanexus/status/860190148239081472 https://twitter.com/ramyanexus/status/860190148239081472 If you want to configure F5 to launch tests instead of your main entry point, you can do that too in your launch.json file.
- Arnavion 9y ago>`a | b` can be written as: `if !a && b { a = true }` `a || b` ... The repository does have multiple cases of `a || b` and none of the `a = true` kind, so perhaps the author realized it after writing this blog post.
- thetic 9y agoThis one tripped me up too. I'm guessing he meant `a |= b`.
- Arnavion 9y agoOf course he meant that. My point was that `if !a && b { a = true; }` is a silly way to write `a = a || b;`
- teolandon 9y agoIt's right on the line wrap, but it actually says `a = a | b`. At least right now, not sure if they edited it.
- nv-vn 9y agoNot naming it Yogo was a missed opportunity.
- krylon 9y ago> However, win32 doesn’t have anything to help you layout things on the screen. Manual positioning is painful. Ooooh, yes, I remember that. My first experiences with GUI programming was a tiny little of Perl/Tk, then Gtk 2 in Perl, Python, and Ruby. And then I started working as programmer, doing maintenance on an line-of-business application suite written in C/Win32, and I was shocked people would actually put up with this. Fortunately, the overwhelming majority of my work dealt with the deepest guts of those programs, and I only needed to deal with Win32 directly on a few occasions.
- sjmulder 9y agoI've started writing some Win32 code for fun and like you I found it a whole different experience from GTK and such. It really is an API rather than a toolkit. Take scrolling. Scroll bars are provided when requested, but all the interactions have to be implement by the application. There's no 'scroll view' with standard behaviours. This means Microsoft can't easily add new scrolling behaviours as we see with the messy situation around high precision trackpads and touch. Compare this to Cocoa with its auto layout, data binding and even document lifecycle management. On the other hand, with Win32 I rarely find myself fighting the toolkit. I'm in control. Backwards comatibility is also amazing. With my little project I'm targeting Windows 3.1 through Windows 10 with a single binary! I expected most of my effort to go towards supporting old OSes, but in reality I spend more time on modern features like proper per-monitor DPI suport, the gazillion different scrolling behaviours and (conditionally loaded) GDI+ and Direct2D implementations. Another delightful aspect, although again it comes the beforementioned price, is the speed. My little utility starts instantly and won't skip a beat when you frantically shake the resize grip. Compare that to even the simplest UWP XAML app.
- krylon 9y ago> Another delightful aspect, although again it comes the beforementioned price, is the speed. That is true, with only a little care, one can write insanely fast and responsive UIs in Win32.
- aninteger 9y agoIt's funny you mention that. I grew up with the Windows API first and then moved to Tcl/To, and then Swing. I remember wishing I could just manually position things instead of packing into horizontal and vertical boxes. Maybe it is Stockholm syndrome with the Windows API for me.
- kristianp 9y agoLooks like they're using walk and win for the gui. https://github.com/kjk/walk https://github.com/kjk/walk https://github.com/kjk/win https://github.com/kjk/win
- jclulow 9y ago> Too many people have a bad habit of including “go” in their repository name, presumably to not confuse people with non-go version of the code. Note that this is actually a perfectly reasonable thing to do. If I had two clients for some particular API, one written for Node and one for Go, it absolutely makes sense to name them "node-whatever.git" and "go-whatever.git". Part of what makes naming things hard is needing to come up with a more "creative" name for every little module, especially once you throw in the constraint that apparently eight characters is too long and four characters is just right.
- kjksf 9y agoIt might be necessary to resolve naming conflicts but it's not good. In Go, package name should be the same as repo name. Most cases that I've seen are not including "go" in repo name to resolve naming conflict but "just because", possibly cargo-culting others that did it.
- jclulow 9y agoWhat if those people or organisations have a lot of repositories and they want to use a naming convention that reflects that maybe, just maybe, only a subset of their code is written in Go? I recognise that you don't speak for the entire Go community, but frankly this kind of over-prescriptive preference presented as the only possible way forward is oddly common in what often seems more like a church than a programming language ecosystem. Go is just a tool, like any other. If I happen to use git for versioning that's my choice, and I (and everybody else) can and should name and arrange repositories as is convenient. There is no "in Go" to be had here; merely "in my project as makes sense for me".
- kjksf 9y agoIf you must then you must. I'm not sure if you actually disagree with me. Like I said, most of the time I see "go" in project name, it's not because the owner of the repo was in the hypothetical situation you describe. So unless you think it's ok to add "go" to the name of the repo willy-nilly there's no disagreement.
- 43224gg252 9y agoA title mentioned Go, so I guess that means the Elixir/Erlang marketing team is going to come in and tell us why Elixir is better. Yawn. It's as obvious as the monsanto shills.
- j_s 9y agoSweet, Windows GUI in Go! Glad I put this off this long... PS. That evergreen/un-dated content tho!
- Sembiance 9y agoToday I learned that Go does not support the ternary operator. that's sad :(
- CamperBob2 9y agoOr bitwise OR and XOR operators, according to the article? That's a bit hard to believe, no pun intended.
- Beanis 9y agoBitwise OR and XOR are supported on ints. It doesn't work on bools, but there are equivalents: https://play.golang.org/p/GCAFwx03pi https://play.golang.org/p/GCAFwx03pi
- plorkyeran 9y agoBitwise OR and XOR on bools. Bitwise XOR is just !=, so it's useless. Bitwise OR isn't identical to any other operators, but writing code which specifically relies on side-effects of a function on the RHS of an OR does not strike me as the sort of thing that the Go designers would approve of.
- deleted 9y ago[deleted]
- CamperBob2 9y agoBitwise OR is equivalent to addition, but IMHO it makes more sense to omit support for addition on bools than to omit support for the classical boolean operators supported by C++. If for no other reason than to make existing C++ code easier to port.
- wbl 9y agoAddition on bools is XOR.
- 9y ago
- deleted 9y ago[deleted]
- BelleMe 9y agohttps://pitbullandgoldfishes.wordpress.com https://pitbullandgoldfishes.wordpress.com
- st3fan 9y ago> As far as I can tell you can’t step through tests in the debugger Really? In Visual Studio Code this is as simple as: 1) Set a breakpoint in your test 2) Hit the "Debug Test" button See http://wopr.norad.org/~stefan/delve.png http://wopr.norad.org/~stefan/delve.png for this in action. Look how great this looks. And how you can see all your variables in the upper left section. How you can expand structs and inspect them. In my experience this works really really well.
- lobster_johnson 9y agoDelve and the VSC integration is great. Just note that it's historically been quite buggy and unstable. Worse, for a long time it wasn't practically possible to use Delve on macOS due to changes introduced in 10.12 [1] [2]. It should be better now, though. [1] https://github.com/derekparker/delve/issues/645 https://github.com/derekparker/delve/issues/645 [2] https://github.com/aaronhackney/delve_on_mac/blob/master/README.md https://github.com/aaronhackney/delve_on_mac/blob/master/REA...
- mkup 9y agoThere's a C++ library for Win32 (and other platforms) GUI programming with built-in layout engine, wxWidgets. It's a bit old, but works quite good, large DPI aware etc. Unlike Qt, it aims to be slim, provides native look and feel for all controls on all platforms, and avoids creating its own leaky abstractions. Necessary workarounds for old versions of Windows are included too. Probably it would be easier to create Go language binding for wxWidgets rather than reimplementing all that stuff from scratch. wxWidgets already has Python, Perl and Ruby bindings, and besides Windows supports Mac (Carbon and Cocoa) and Linux (GTK, X11 and OpenMotif).
- jeremyjh 9y agoThere are already wrapper libraries in Go for it. I'd say more relevant to the article is the fact that wxBoxSizer wxFlexGridSizer are reportedly similar in capability to flexbox.
- 4ad 9y agoGo is so close to C. Wouldn’t it be great if there was a program that could take C code and turn it into Go code? There are few attempts to do that: https://github.com/rsc/c2go https://github.com/elliotchance/c2go https://github.com/cznic/ccgo elliotchance/c2go seems to be the most promising. I didn’t try any of them as neither seems to be usable yet. Actually, we used github.com/rsc/c2go to port our Go compilers and linkers from C to Go. It was 100% mechanical translation. No manual steps needed to achieve correctness. github.com/cznic/ccgo was used to port sqlite to Go. It passes all the (very comprehensive) sqlite test suite. They work very well. The code produced is not very idiomatic Go, but it's not some monstrous impenetrable computer generated code either. It's quite similar to the original C code and it's readable and refactorable enough. After we converted the compiler from C to Go, we spent a lot of time to make the code more idiomatic.
- WaltPurvis 9y ago>github.com/cznic/ccgo was used to port sqlite to Go. Where is the result of this? Is it publicly available?
- 4ad 9y agoSure: https://godoc.org/github.com/cznic/sqlite https://godoc.org/github.com/cznic/sqlite https://github.com/cznic/sqlite https://github.com/cznic/sqlite
- fithisux 9y agoMy only problem with porting from A to B is how to keep B updated when A changes. Manual step destroys it all.
- thelazydogsback 9y ago> "Go doesn’t support xor (^) and or (|) operator on bool. As it turns out, they are not neccessary. x ^ y is the same as x != y. a = a | b can be written as: if !a && b { a = true }" Well, you don't need subtraction, division or multiplication either (or any number more than 1!), but they're certainly nice to have. Go has some nice qualities, but its choice of omissions continues to fascinate me.
- tomjakubowski 9y agoYou can put (some of?) your fascination to rest, Go has ||. https://play.golang.org/p/_GKbk_GUAe https://play.golang.org/p/_GKbk_GUAe