Thoughts, stories and ideas.

Generator Token

文字列からtoken生成

package main

import "fmt"  
import "crypto/md5"  
import "encoding/hex"

func main() {  
    data := []byte("hello world")
    b := md5.Sum(data)
    pass := hex.EncodeToString(b[:])
    fmt.Println(pass)  // 5eb63bbbe01eeed093cb22bb8f5acdc3 
}

defer

deferについての動作確認

package main

import "fmt"

func main() {  
    defer fmt.Println("defer main func") 
    hoge()
    fmt.Println("Hello, playground")

}

func hoge(){  
    fmt.Println("hoge func")
    defer fmt.Println("defer hoge func1") 
    defer fmt.Println("defer hoge func2") 
}
hoge func  
defer hoge func2  
defer hoge func1  
Hello, playground  
defer main func  

Image Download

Image URLからファイルをダウンロードして保存

url := "http://hoge.com/hoge.png"

response, err := http.Get(url)  
if err != nil {  
    panic(err)
}
defer response.Body.Close()

fmt.Println("status:", response.Status)

storePath := "/images/photo.png"

file, err := os.OpenFile(storePath, os.O_CREATE|os.O_WRONLY, 0666)  
if err != nil {  
    panic(err)
}
defer file.Close()

io.Copy(file, response.Body)  

base64 string

Fileからbase64文字列生成

file, err = os.Open("/data/photo.png")  
if err != nil {  
    panic(err)
}
defer file.Close()

fi, _ := file.Stat()  
size := fi.Size()

data := make([]byte, size)  
file.Read(data)

base64String := base64.StdEncoding.EncodeToString(data)  
fmt.Println(base64String)  

数字 <=>文字列

import "strconv"

// int -> string
stringVal := strconv.Itoa(intVal)

// string -> int
intVal, err := string.Atoi(stringVal)

// stringValが数字ではない時にエラー発生
intVal, err := string.Atoi("abc")  
fmt.Println(err) // strconv.ParseInt: parsing "abc": invalid syntax

Golang Error 作成

カスタマイズされたErrorを作ってみる

// MyError ...
type MyError struct {  
    Code    string
    Message string
}

func (e MyError) Error() string {  
    return fmt.Sprintf("MyError Code=%s : Message=%s", e.Code, e.Message)
}

// 使う側(なんかの処理)
if hasError == true {  
    return nil, &MyError{"E001", "お前が指定してるcodeは存在しない!"}
}

文字列だけのErrorだと以下のように書ける

errors.New("なんとなくエラーです")  

Welcome to Ghost

You're live! Nice. We've put together a little post to introduce you to the Ghost editor and get you started. You can manage your content by signing in to the admin area at <your blog URL>/ghost/. When you arrive, you can select this post from a list on the left and see a preview of it on the right. Click the little pencil icon at the top of the preview to edit this post and read the next section!

Getting Started

Ghost uses something called Markdown for writing. Essentially, it's a shorthand way to manage your post formatting as you write!

Writing in Markdown is really easy. In the left hand panel of Ghost, you simply write as you normally would. Where appropriate, you can use shortcuts to style your content. For example, a list:

  • Item number one
  • Item number two
    • A nested item
  • A final item

or with numbers!

  1. Remember to buy some milk
  2. Drink the milk
  3. Tweet that I remembered to buy the milk, and drank it

Want to link to a source? No problem. If you paste in a URL, like http://ghost.org - it'll automatically be linked up. But if you want to customise your anchor text, you can do that too! Here's a link to the Ghost website. Neat.

What about Images?

Images work too! Already know the URL of the image you want to include in your article? Simply paste it in like this to make it show up:

The Ghost Logo

Not sure which image you want to use yet? That's ok too. Leave yourself a descriptive placeholder and keep writing. Come back later and drag and drop the image in to upload:

Quoting

Sometimes a link isn't enough, you want to quote someone on what they've said. It was probably very wisdomous. Is wisdomous a word? Find out in a future release when we introduce spellcheck! For now - it's definitely a word.

Wisdomous - it's definitely a word.

Working with Code

Got a streak of geek? We've got you covered there, too. You can write inline <code> blocks really easily with back ticks. Want to show off something more comprehensive? 4 spaces of indentation gets you there.

.awesome-thing {
    display: block;
    width: 100%;
}

Ready for a Break?

Throw 3 or more dashes down on any new line and you've got yourself a fancy new divider. Aw yeah.


Advanced Usage

There's one fantastic secret about Markdown. If you want, you can write plain old HTML and it'll still work! Very flexible.

That should be enough to get you started. Have fun - and let us know what you think :)