Skip to content

Instantly share code, notes, and snippets.

@trknhr
Last active June 28, 2017 04:30
Show Gist options
  • Save trknhr/d77759bde132f16aab79efa8c85d45da to your computer and use it in GitHub Desktop.
Save trknhr/d77759bde132f16aab79efa8c85d45da to your computer and use it in GitHub Desktop.
Golang LT 2017/06/28
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
var err error
var f string
var b string
f, err = doAction("foo")
check(err)
b, err = doAction("hoo")
check(err)
if f != "" && b != "" {
fmt.Printf("%s%s", f, b)
}
}
func check(err error) {
if err != nil {
fmt.Println(err)
}
}
func doAction(s string) (string, error) {
var message string
if rand.Intn(10) > 2 {
if rand.Intn(10) > 5 {
return "Success" + s, nil
}
return message, nil
}
return message, fmt.Errorf("%s Fail", s)
}
import scalaz._
import scala.util.Random
object EitherSample{
def main(args: Array[String]): Unit ={
println(for {
foo <- doAction("foo")
bar <- doAction("hoo")
} yield{
for{
f <- foo
b <- bar
} yield{
f + b
}
})
}
def doAction(a: String): \/[Exception, Option[String]] = {
val r = new Random
if(r.nextInt(10) > 2) {
\/-(Some("Success" + a))
} else {
-\/(new Exception())
}
}
}
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
class FileRead{
public static void main(String args[]){
BufferedReader br = null;
FileReader fr = null;
final String FILE_NAME = "editorToMemo.txt";
try {
fr = new FileReader(FILE_NAME);
br = new BufferedReader(fr);
String fileString;
br = new BufferedReader(new FileReader(FILE_NAME));
while ((fileString = br.readLine()) != null) {
System.out.println(fileString);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
package main
import "fmt"
type MyStruct struct {
flag bool
num int
}
func main() {
results := []MyStruct{MyStruct{true, 10}, MyStruct{false, 9}, MyStruct{true, 99}, MyStruct{true, 3}}
sum := 0
for _, r := range results {
j, k := r.flag, r.num
if !j || k%2 == 0 {
continue
}
sum += k
}
fmt.Println(sum)
}
let results = [{flag: true, num: 10}, {flag: false, num: 9}, {flag: true, num: 99}, {flag: true, num: 3}]
results.filter(a => a.flag && a.num % 2 !== 0).reduce((acc, a) => acc + a.num, 0)
package main
import (
"fmt"
"io"
"os"
)
func main() {
f, err := os.Open("./editorToMemo.txt")
if err != nil {
panic(err)
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
// make a buffer to keep chunks that are read
buf := make([]byte, 1024)
for {
// read a chunk
n, err := f.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
fmt.Println(string(buf))
if n == 0 {
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment