This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; @snippet.elisp.string | |
(defun str-at-point () | |
"read string in quotes" | |
(interactive) | |
(save-excursion | |
(let ($skipchars $p1 $p2) | |
(setq $skipchars "^'\"`") | |
(skip-chars-backward $skipchars) | |
(setq $p1 (point)) | |
(skip-chars-forward $skipchars) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @snippet.string convert to camel case | |
func ToCamelCase(input string) string { | |
titleSpace := strings.Title(strings.Replace(input, "_", " ", -1)) | |
camel := strings.Replace(titleSpace, " ", "", -1) | |
return strings.ToLower(camel[:1]) + camel[1:] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @snippet.string pad string | |
func PadString() { | |
needSpace := "need space" | |
fmt.Println(pad(needSpace, 14, "CENTER")) | |
fmt.Println(pad(needSpace, 14, "LEFT")) | |
} | |
func pad(input string, padLen int, align string) string { | |
inputLen := len(input) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @snippet.string trim white space | |
func StringTrimeSpace() { | |
stringToTrim := "\t\t\n Go \tis\t Awesome \t\t" // | |
trimResult := strings.TrimSpace(stringToTrim) | |
fmt.Println(trimResult) //Go is Awesome | |
} | |
// @snippet.string.regex replace whitespace with regex | |
func StringTrimRegex() { | |
stringWithSpaces := "\t\t\n Go \tis\n Awesome \t\t" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @snippet.logging log to syslog | |
func LogToSyslog() { | |
sysLog, err := syslog.New(syslog.LOG_SYSLOG, "systemlog.go") | |
if err != nil { | |
log.Println(err) | |
return | |
} else { | |
log.SetOutput(sysLog) | |
log.Print("Everything is fine") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @snippet.string indexOf substring | |
func IndexOfSubstring() { | |
strings.Index("Japan", "abc") //-1 Index of first abc | |
strings.IndexAny("Japan", "abc") // 1 a,b or c | |
strings.LastIndex("Japan", "abc") // -1 index of last abc | |
strings.LastIndexAny("Japan", "abc") // 3 a,b or c | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @snippet.string.sort sort string | |
func SortString() { | |
w := "anagram" | |
s := strings.Split(w, "") | |
sort.Strings(s) | |
res := strings.Join(s, "") | |
fmt.Printf("SortString(): %v\n", res) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @snippet reverse string | |
func StrReverse(s string) string { | |
var sb strings.Builder | |
for i := len(s) - 1; i >= 0; i-- { | |
fmt.Fprintf(&sb, "%c", s[i]) | |
} | |
return sb.String() | |
} | |
func ReverseString(s string) string { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func ScriptExecutionTime() { | |
start := time.Now() | |
fmt.Println("The Script started at :", start) | |
fmt.Println("Saving the world....") | |
time.Sleep(2 * time.Second) | |
end := time.Now() | |
fmt.Println("The Script completed at: ", end) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @snippet.string.number convert string to numbers | |
func ConvertStringToNumber() { | |
const bin = "00001" | |
const hex = "2f" | |
const intString = "12" | |
const floatString = "12.3" | |
// @snippet.number parse integer Decimals | |
res, err := strconv.Atoi(intString) | |
if err != nil { |