ubuntuのgolangをアップデート

ubuntuのgolangをアップデートします
vagrant@vagrant-ubuntu-trusty-64:~/ethereum$ go env
GOARCH=”amd64″
GOBIN=””
GOCHAR=”6″
GOEXE=””
GOHOSTARCH=”amd64″
GOHOSTOS=”linux”
GOOS=”linux”
GOPATH=””
GORACE=””
GOROOT=”/usr/lib/go”
GOTOOLDIR=”/usr/lib/go/pkg/tool/linux_amd64″
TERM=”dumb”
CC=”gcc”
GOGCCFLAGS=”-g -O2 -fPIC -m64 -pthread”
CXX=”g++”
CGO_ENABLED=”1″
vagrant@vagrant-ubuntu-trusty-64:~/ethereum$ sudo apt-get update
vagrant@vagrant-ubuntu-trusty-64:~/ethereum$ sudo apt-get install gcc make
vagrant@vagrant-ubuntu-trusty-64:~/ethereum$ sudo apt-get install golang-1.10
vagrant@vagrant-ubuntu-trusty-64:~/ethereum$ ls /usr/lib | grep go
go
go-1.10
gold-ld
vagrant@vagrant-ubuntu-trusty-64:~/ethereum$ cd /usr/bin
vagrant@vagrant-ubuntu-trusty-64:/usr/bin$ sudo mv go go_16
vagrant@vagrant-ubuntu-trusty-64:/usr/bin$ sudo mv gofmt gofmt_16
vagrant@vagrant-ubuntu-trusty-64:/usr/bin$ sudo ln -s /usr/lib/go-1.10/bin/go /usr/bin/go
vagrant@vagrant-ubuntu-trusty-64:/usr/bin$ sudo ln -s /usr/lib/go-1.10/bin/gofmt /usr/bin/gofmt
vagrant@vagrant-ubuntu-trusty-64:/usr/bin$ go version
go version go1.10.4 linux/amd64

OK^^

再度makeします
vagrant@vagrant-ubuntu-trusty-64:~/ethereum$ make -C go-ethereum geth

/usr/lib/go-1.10/pkg/tool/linux_amd64/link: -X flag requires argument of the form importpath.name=value

何いいいいいいいいいいいいい

Ethereumのインストール

ubuntuにgo言語実装のgo-ethereumを入れる
vagrant@vagrant-ubuntu-trusty-64:~$ mkdir ethereum
vagrant@vagrant-ubuntu-trusty-64:~$ ls
ethereum
vagrant@vagrant-ubuntu-trusty-64:~$ cd ethereum
vagrant@vagrant-ubuntu-trusty-64:~/ethereum$ sudo apt-get install -y build-essential libgmp3-dev golang

vagrant@vagrant-ubuntu-trusty-64:~/ethereum$ sudo apt-get install git
vagrant@vagrant-ubuntu-trusty-64:~/ethereum$ git version
git version 1.9.1

vagrant@vagrant-ubuntu-trusty-64:~/ethereum$ git clone -b release/1.3.6 https://github.com/ethereum/go-ethereum.git

おおお

vagrant@vagrant-ubuntu-trusty-64:~/ethereum$ make -C go-ethereum geth
make: Entering directory `/home/vagrant/ethereum/go-ethereum’
build/env.sh go install -v -ldflags ‘-X main.gitCommit bf324bd24be9036046e36c6248b2d7c31cce9eca’ ./cmd/geth
github.com/codegangsta/cli
github.com/ethereum/go-ethereum/rlp
# github.com/ethereum/go-ethereum/rlp
build/_workspace/src/github.com/ethereum/go-ethereum/rlp/encode.go:162: undefined: sync.Pool
github.com/ethereum/go-ethereum/crypto/ecies
github.com/ethereum/go-ethereum/crypto/sha3
github.com/ethereum/go-ethereum/crypto/randentropy
github.com/ethereum/go-ethereum/crypto/secp256k1
github.com/pborman/uuid
golang.org/x/crypto/pbkdf2
golang.org/x/crypto/ripemd160
golang.org/x/crypto/scrypt
github.com/ethereum/go-ethereum/logger/glog
github.com/ethereum/go-ethereum/params
github.com/hashicorp/golang-lru
github.com/rcrowley/go-metrics
github.com/syndtr/goleveldb/leveldb/util
github.com/syndtr/goleveldb/leveldb/cache
github.com/syndtr/goleveldb/leveldb/comparer
github.com/syndtr/goleveldb/leveldb/storage
github.com/syndtr/goleveldb/leveldb/errors
github.com/syndtr/goleveldb/leveldb/filter
github.com/syndtr/goleveldb/leveldb/iterator
github.com/syndtr/goleveldb/leveldb/journal
github.com/syndtr/goleveldb/leveldb/memdb
github.com/syndtr/goleveldb/leveldb/opt
github.com/syndtr/gosnappy/snappy
github.com/syndtr/goleveldb/leveldb/table
github.com/syndtr/goleveldb/leveldb
gopkg.in/karalabe/cookiejar.v2/collections/prque
github.com/ethereum/go-ethereum/event
gopkg.in/fatih/set.v0
github.com/huin/goupnp/httpu
github.com/huin/goupnp/scpd
github.com/huin/goupnp/soap
github.com/huin/goupnp/ssdp
golang.org/x/net/html/atom
golang.org/x/net/html
golang.org/x/text/transform
golang.org/x/text/encoding
golang.org/x/text/encoding/internal/identifier
golang.org/x/text/encoding/internal
golang.org/x/text/encoding/charmap
golang.org/x/text/encoding/japanese
golang.org/x/text/encoding/korean
golang.org/x/text/encoding/simplifiedchinese
golang.org/x/text/encoding/traditionalchinese
golang.org/x/text/encoding/unicode
golang.org/x/net/html/charset
github.com/huin/goupnp
# github.com/huin/goupnp
build/_workspace/src/github.com/ethereum/go-ethereum/Godeps/_workspace/src/github.com/huin/goupnp/goupnp.go:113: unknown http.Client field ‘Timeout’ in struct literal
github.com/jackpal/go-nat-pmp
github.com/ethereum/go-ethereum/event/filter
github.com/robertkrimen/otto/file
github.com/robertkrimen/otto/token
github.com/robertkrimen/otto/ast
github.com/robertkrimen/otto/dbg
github.com/robertkrimen/otto/parser
github.com/robertkrimen/otto/registry
github.com/robertkrimen/otto
github.com/rs/cors
github.com/peterh/liner
github.com/mattn/go-isatty
github.com/shiena/ansicolor
github.com/fatih/color
github.com/mattn/go-runewidth
github.com/nsf/termbox-go
github.com/gizak/termui
make: *** [geth] Error 2
make: Leaving directory `/home/vagrant/ethereum/go-ethereum’

vagrant@vagrant-ubuntu-trusty-64:~/ethereum$ go version
go version go1.2.1 linux/amd64

https://github.com/ethereum/go-ethereum

Building geth requires both a Go (version 1.10 or later) and a C compiler. You can install them using your favourite package manager. Once the dependencies are installed, run

あれ?

Go API sample

package main

import (
	"flag"
	"fmt"
	"log"

	"code.google.com/p/google-api-go-client/youtube/v3"
)

func main(){
	flag.Parse()

	client, err :=buildOAuthHTTPClient(youtube.YoutubeReadonlyScope)
	if err != nil {
		log.Fatelf("Error creating YouTube client: %v", err)
	}

	call := service.Channels.List("contentDetails").Mine(true)

	resoponse, err := call.Do()
	if err != nil {
		log.Fatalf("Error making API call to list channels: %v", err.Error())

,	channel := range response.Items {
		playlistId := channel.ContentDetails.RelatedPlaylists.Uploads
		fmt.Printf("Videos in list %s\r\n", playlistId)

		nextPageToken := ""
		for {
			playlistCall := service.PlaylistItems.List("snippet").
				PlaylistId(playlistId) .
				MaxResults(50) .
				PageToken(nextPageToken)
			playlistResponse, err := playlistCall.Do()

			if err != nil {
				log.Fatelf("Error fetching playlist items: %v", err.Error())
			}

			for _, playlistItem := range playlistResponse.Items {
				title := playlistItem.Snippet.Title
				videoId := playlistItem.Snippet.ResourceId.VideoId
				fmt.Printf("%v, (%v)\r\n", title, videoId)
			}

			nextPageToken = playlistResponse.NextPageToken
			if nextPageToken == ""{
				break
			}
			fmt.Println()
		}
	}
}
}

A Tour to Go

The rule is that public functions, types, etc., should be upper case.

Variable

package main

import "fmt"

func main(){
  var msg string
  msg = "hello world"
  fmt.Println(msg)

 msg := "hello world"
  fmt.Println(msg)

  a, b:= 10, 15
}

基本データ型

package main
import "fmt"

func main(){
  a := 10
  b := 12.3
  c := "hoge"
  var d bool
  fmt.Printf("a: %d, b:%f, c:%s, d:%t\n", a, b, c, d)
}

const, iota: https://github.com/golang/go/wiki/Iota

package main
import "fmt"

func main(){
  // const name = "yoshida"
  // name = "tanaka"

  const (
    sun = iota
    mon
    tue
  )
  fmt.Println(sun, mon, tue)
}

pointer

package main
import "fmt"

func main(){
  a := 5
  var pa *int
  pa = &a // a address
  fmt.Println(pa)
  fmt.Println(*pa)
}

function

package main
import "fmt"

func hi(name string) string{
  msg := "hi!" + name
  return msg
}

func main(){
  fmt.Println(hi("tanaka"))
}

swap

package main
import "fmt"

func swap(a, b int)(int, int){
  return b, a
}

func main(){
  fmt.Println(swap(5, 2))
}

array

a := [...]int{1, 3, 5}
a := [5]int{1, 3, 5, 8, 9}
  s := a[2:4]
  fmt.Println(s)
  fmt.Println(len(s))
  fmt.Println(cap(s))
  s := []int{1, 3, 5}
  s = append(s, 8, 2, 10)
  t := make([]int, len(s))
  n := copy(t, s)

map

  m := make(map[string]int)
  m["tanaka"] = 200
  m["sato"] = 300
  fmt.Println(m)

if

score := 83
  if score > 80 {
    fmt.Println("great")
  } else {
    fmt.Println("soso")
  }

range

s := []int{2, 3, 8}
  for i, v := range s{
    fmt.Println(i, v)

構造体

type user struct {
  name string
  score int
}

func main(){
    u := new(user)
    u.name = "tanaka"
    fmt.Println(u)

}

go routine

package main
import (
  "fmt"
  "time"
  )

func task1(){
  time.Sleep(time.Second * 2)
  fmt.Println("task1 finished!")
}
func task2(){
  fmt.Println("task2 finished!")
}

func main(){
  go task1()
  go task2()

  time.Sleep(time.Second * 3)
}

Getting started with Go lang

Go is a cool programming language. Check out golang.org

install:sudo yum install golang

[vagrant@localhost go]$ go version
go version go1.7.3 linux/amd64
package main
import "fmt"

func main(){
  fmt.Println("hello world")
}

Go言語では、go buildでコンパイルしますが、go run xxx.goの1行で、実行することも可能です。

[vagrant@localhost go]$ go build hello.go
[vagrant@localhost go]$ ls
hello  hello.go
[vagrant@localhost go]$ ./hello
hello world
[vagrant@localhost go]$ go run hello.go
hello world