Here are some example code:
func main() {
os.MkdirAll(outDir + id, 0755)
os.Create(outDir + id + "/txt")
os.OpenFile(outDir + id + "/" + ".tmp", os.OWRONLY|os_APPEND)
os.Stat(outDir + id + "/.tmp")
}
The following is the output after formatting with either go fmt or pressing Format on the Go Playground:
func main() {
os.MkdirAll(outDir+id, 0755)
os.Create(outDir + id + "/txt")
os.OpenFile(outDir+id+"/"+".tmp", os.OWRONLY|os_APPEND)
os.Stat(outDir + id + "/.tmp")
}
Spaces in os.MkdirAll() and os.OpenFile() are removed while they are untouched in os.Create() and os.Stat(). I would expect that formatting to be identical.
Why is this happening?
See: https://github.com/golang/go/issues/12720
gofmt uses spaces around binary expressions to express binding
strength. Depending on nesting level, spaces are removed.
You could also find these easily by searching for "gofmt inconsistent spaces".
See also issue #1206, #1848, #1861, #7880, and #11497.
Related
I created a custom Logger for logging values for different modes like debug, release but I want that whenever I use the default print() Xcode should throw an error explaining the user to use the custom logger.
I referred this but want to throw error there and then rather than adding a build-phase.
Any Ideas ?
You could shadow the built-in print in your module and mark it "unavailable":
// swift 3:
#available(*, unavailable, message: "use Logger instead!")
internal func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
Swift.print(items, separator: separator, terminator: terminator)
}
// swift 2:
#available(*, unavailable, message="use Logger instead!")
internal func print(items: Any..., separator: String = " ", terminator: String = "\n") {
Swift.print(items, separator: separator, terminator: terminator)
}
The result looks like:
(There is still another instance of print<Target>(..., to output: inout Target) which I don't bother to hide, I guess no one will accidentally use that function.)
As shown, you could still use the qualified name Swift.print to refer to the real function in case of emergency.
Note that this will only affect your own module. You can't force users outside of your module to not use print.
So, I'm teaching myself some Golang by making a simple resource management game with ncurses. I'm using this library to connect Golang to ncurses.
I've made a simple text input panel that takes in one character at a time, displays it, and then adds it to a string composing the user's response. Here's what it looks like:
// Accept characters, printing them until end
ch := window.GetChar()
kstr := gc.KeyString(ch)
response := ""
cur := 0
for kstr != "enter" {
// Diagnostic print to get key code of current character
window.Move(0,0)
window.ClearToEOL()
window.MovePrint(0, 0, ch)
// If its a backspace or delete, remove a character
// Otherwise as long as its a regular character add it
if ((ch == 127 || ch == 8) && cur != 0){
cur--
response = response[:len(response)-1]
window.MovePrint(y, (x + cur), " ")
} else if (ch >= 33 && ch <= 122 && cur <= 52) {
window.MovePrint(y, (x + cur), kstr)
response = response + kstr
cur++
}
// Get next character
ch = window.GetChar()
kstr = gc.KeyString(ch)
}
However, the arrow and function keys seem to be coming up as keycodes already associated with the normal a-zA-Z characters. For example, right-arrow comes up as 67 and F1 as 80. Any ideas what I'm doing wrong here, or if there's a better approach to taking in alphanumerics through ncurses? I'd like to avoid ncurses fields and classes as much as possible, because the point here is to learn Golang, not ncurses. Thanks!
If you do not enable the keypad mode, (n)curses will return the individual bytes which make up a special key.
To fix, add this to your program's initialization:
stdscr.Keypad(true) // allow keypad input
which will return special keys such as right-arrow as values above 255. goncurses has symbols defined for those, e.g., KEY_RIGHT.
In pre-Swift 2.0 sample code, I've come across something like:
var val = "hello" + Repeat(count: paddingAmount, repeatedValue: "-") + "."
In Xcode 7.0/Swift 2.0 Playground, this produces the error:
note: expected an argument list of type '(String, String)'
How would you use the Repeat collection and get the value that's held by the collection for use?
String has an initializer that will return a string of repeated characters, I would recommend using that in your case:
let padding = String(count: paddingAmount, repeatedValue: Character("-"))
var val = "hello" + padding + "."
It's now Array(count: paddingAmount, repeatedValue: "-").
Hello i would like to create a app that changes characters into binary code and i was wondering if there is a way to add multiple stringByReplacingOccurrencesOfString on one String or if i should take another approach to this "Problem".
Here is what i have so far
func textToBinary(theString: String) -> String {
return theString.stringByReplacingOccurrencesOfString("a",
withString: "01100001")
}
textArea.text = textToBinary(lettersCombined)
// lettersCombined is the string that i want to turn into BinaryCode.
Try this:
func textToBinary(theString : String, radix : Int = 2) -> String {
var result = ""
for c in theString.unicodeScalars {
result += String(c.value, radix: radix) + " "
}
return result
}
println(textToBinary("a"))
println(textToBinary("abc", radix: 10))
println(textToBinary("€20", radix: 16))
println(textToBinary("😄"))
(The last one is a smiley face but somehow my browser can't display it).
Edit: if you want to pad your strings to 8-character long, try this:
let str = "00000000" + String(c.value, radix: radix)
result += str.substringFromIndex(advance(str.startIndex, str.characters.count - 8)) + " "
The first line adds eight 0 the left of your string. The second line takes the last 8 characters from the padded string.
I am setting up an onmouseup event like this:
html += ' onmouseup="' + func + '(event, \'' + tileInfo.targetURL + '\', \'' + tileInfo.id + '\')"
where func is a var with a value like "launchA" or "launchB". The launchA and launchB methods expect three arguments, event, targetURL (which is a string) and id (which is also a string).
Since both targetURL and id are properties of the same object (tileInfo), I would prefer to pass tileInfo to the launch methods, and let them make references to tileInfo.targetURL and tileInfo.id, but when I set it up like this:
html += ' onmouseup="' + func + '(event, \'' + tileInfo + '\')"
when it hits launchA, tileInfo is an Object, but tileInfo.id is undefined.
More specifics were provided in a separate but related question, which was answered here: Javascript attach new property to element.