70 points · by piranha · 13y ago · 63 comments
find ~/my/docs/ -type f -name '*.txt' -exec sed -i.bak 's/inheritance/composition/g' {} +
This will search all files ending in `.txt` and will exec `sed 's/inheritance/composition/g'` on it. Sed modifies files in-place and saves a backup file with .bak extension (-i.bak) and is called the minimum necessary times (-exec +).My best advice to someone using the command line is to learn `find` + `xargs` or, even better, `find` + `parallel`.
# bash
# find/replace
function fr {
local pattern=$1
local replacement=$2
local program
if [[ -z "$replacement" ]]
then
program="grep $pattern"
else
program="sed -i s/$pattern/$replacement/g"
fi
while read line
do
$program "$line"
done
}
# usage
find . | fr find-this
find . | fr replace-this with-that1. You've imported droundy/goopt, wsxiaoys/terminal really easily. The first provides option-parsing, and the second provides helpers for emitting ANSI escape codes.
2. Practically everything is overloaded. You can == to strcmp(), + to concatenate strings.
3. path/filepath gives you some nice goodies for path manipulation. But you have to deal with the fallouts of making naive assumptions like whether or not to recurse symlinks (why isn't this a flag in filepath.Walk?). This has resulted in ugliness in your walkFunc callback.
4. You have been able to abstract quite a bit without using Java-like factories, although your inconsistent error passing has me somewhat confused.
5. Since it's so strongly typed and there are no raw pointers, it's not going to be hard to debug at all.
Overall, I'd say Go is an effective language. But not beautiful, or novel in the slightest. It resembles Algol-68 (yes, a 1968 language) quite strongly, and I don't think it will drive compiler technology. Their syntax is specifically engineered to be parseable quickly (and hence super-fast AOT compilation): what the final program looks like is secondary concern. I'm not sure I see the appeal.
[edit: clarified that the note on compiler technology was an opinion]
That was my first somewhat big program in Go plus coming from Python made a bit of hustle here. I still believe exceptions to be much better than this error-returning. :\ Never learned to do it nicely. :\
> Overall, I'd say Go is an effective language. But not beautiful, or novel in the slightest.
Yes, this is more or less my feelings nowadays. It's good for certain cases and if I ever need something fast, I'll use Go and not C.
> I'm not sure I see the appeal.
There is some. It's still much simpler than C (GC + stricter compiler), plus it has goroutines (not much in this app, though), plus interfaces are really wonderful (you don't need to declare that you implement interface, just implement it and you're done).
On Go. It'll take time for a programmer to fully understand how to use a new language, and it'll be a long time before great implementations appear. If the language isn't that much of an improvement, why will an existing C programmer take the effort to learn it? Aren't the returns diminishing? Computers will only get faster, llvm will only improve (tooling + compile time + link time): in that respect, doesn't Go seem a bit short-sighted?
One needs to explicitly manage memory, deal with raw pointers, ugly header files and all that stuff. No interfaces, too.
You are right on target. Go was designed to be boring[1][2]. Programming language should not be about tricks and traps. It's a tool. Tools are not built for beauty. They are built for functionality.
[1]http://talks.golang.org/2012/splash.article [2]http://aeronotix.pl/blog/go-is-boring
In [1](2.Introudction) even the inventors agree that Go can be boring to some people. Guess what, It's boring by design.
Where do they say anything of the kind?
"proudly declares that it will not use any modern advances to compiler technology"
Where? I've read most of the available documentation, blog-posts etc. and have yet to come across a statement even vaguely resembling this.
"what the final program looks like is secondary concern"
Have you read about "go fmt"? The entire idea of it is to format the code to a standard. You don't need to use it, but it's recommended and, frankly, makes every other language without a similar feature seem like "what the final program looks like is secondary concern".
You're welcome to your opinion, but I think you'd best do some more research before making such statements without references.
My apologies: this is an opinion, rather than an explicit declaration from the project. Their compiler "gc" starts with a traditional recursive descent parser using bison, and chose not to use llvm. Although one can argue that llvm doesn't provide enough information to implement a great jit, all aot compilers are moving towards using it. By having a common compiler infrastructure, compilers can focus on what is important. They currently have a simple parallel mark-and-sweep garbage collector, and there is some scope for development in the context of concurrent programs (wasn't it their goal to make it easy to write concurrent programs with goroutines?). Nothing spectacular though: the garbage collector in a mature JVM or V8 (which has to deal with a weakly typed language!) is far ahead; that's where the garbage collection research happens.
> what the final programs looks like is secondary concern
The grammar has been specifically engineered so that the language can be analyzed and parsed without a symbol table. "go fmt" is just syntactical sugar: you cannot change the underlying parser. I'm not bikeshedding about which syntax is "better", but pointing out the motivation behind the current syntax. For instance, it's very different from ruby, which was designed ground-up to resemble human language as closely as possible (hence Poignant Ruby etc).
[edit: gc does not use a gcc backend; gccgo does]
Actually, all serious Common Lisp implementations, for example, do incremental AOT compilation these days, and none of them is ever going to "move towards using LLVM". In the broader landscape of programming language implementations, as far as IR generality is concerned, LLVM is actually not significantly better than Java bytecode, and for many languages, trying to use it would probably generate more problems than actually using it would solve.
You'll probably be interested in some of what was talked about in the "Go Fireside Chat" (http://youtu.be/p9VUCp98ay4) in regards to re-designing the compiler, alternate compilers, LLVM etc. It sounds like they're aware of exactly the issues you're concerned about.
Note that specifically for Go source code, there is "go fix -r" for syntax/context-aware code replacement (see "godoc fix").
To be honest, I was just lazy inventing a command line key and it's recursive by design, so it happened this way. Plus, as you say, it's easy to remember because of mnemonics.
I haven't touched svn repos in years, but yeah, that could be added. I know they exist, it just that I haven't felt the need to support them.
go get github.com/piranha/goreplace
go install github.com/piranha/goreplace
is another way to get it if you have GO and GOPATH set up.
I personally aliased it to 'gor' in my zshrc as the git plugin already occupied the 'gr' (short for git remote).
It has something like automatic and manual modes, so you can check (and fix) every diff during the replacement
Also it was written when I had HDD (not SSD) and codebase was on an encrypted image. It made enormous difference to me.
Its a little bit different from Go Replace, giving the option to drop on your editor if the current replacement can't be appied directly (this is the great feature).
C:\Users\swah\docs>gr TEST
panic: Given path should be anchored at /
goroutine 1 [running]:
main.NewIgnorer(0xc08005bcf0, 0x29, 0x0, 0x0, 0x140110, ...)
/Users/piranha/dev/go/src/goreplace/ignore.go:32 +0xa4
main.main()
/Users/piranha/dev/go/src/goreplace/goreplace.go:62 +0x1b7
goroutine 2 [runnable]:
[edit] New version is working! ←[0;32;49mRS\terms.tex
←[0m←[1;39;49m←[0;33;49m25:←[0m ←[0;39;43mOEM←[0m & \\ \hlineEdit: argh, outputting colors on windows means making system calls. Which means cgo and good-bye cross-compiling. I'll probably just strip all colors...