vue-sortable-sandbox
Sotableな要素をVue.jsから扱うサンプル
vue-squire
RichTextEditorのSquireをVue.jsから扱うサンプル
Go言語でテキストの文字数制限を実行するライブラリ
以前Goで文字数制限処理をするのにはどうすればいいのか悩んだのだが、 WEBアプリケーションを作成する際に表示文字数制限はよく利用するのでライブラリとしてまとめてみました。
go-text-export
インストール方法
go get github.com/tkc/go-text-export
簡単な利用方法
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC,
上記のようなテキストに対して、チェインメソッドを実行します。
go_text_export.Create(Str).Export(100).SetMoreText(" more..").RemoveWhiteSpace().String()
結果
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of more..
ほかにも、テキストからリンクを抽出する処理や改行を置換する処理を実装しています。 何気に面倒な処理が楽になります。 試してみてください。
Go Nested Json Unmarshal
GoでNestされたjsonの扱いがわからなかったのでメモ。
jStr := ` { "root": { "c1": ["cv1"], "c2": ["cv2"] } } ` type Children struct { c1 []string `json:"c1"` c2 []string `json:"c2"` } type Root struct { c Children `json:"root"` } var root Root if err := json.Unmarshal([]byte(jStr), &root); err != nil { log.Fatal(err) } log.Printf("%+v\n", root)
このパッケージも良さそう。 github.com
Go からBIgQueryを扱うサンプル
Goを使ってAWSからBIgQueryにアクセスする必要があるときの処理。
package bigquery import ( "log" "reflect" "io/ioutil" "golang.org/x/oauth2" "golang.org/x/oauth2/google" "google.golang.org/api/bigquery/v2" ) const projectId = "***" const dataSetId = "***" const tableName = "***" // Type: // STRING, BYTES, INTEGER, FLOAT, BOOLEAN, TIMESTAMP, DATE, TIME, DATETIME, type TableModel struct { Name string `column:"name" bq_type:"STRING" bq_mode:"NULLABLE"` Address string `column:"address" bq_type:"STRING" bq_mode:"NULLABLE"` } func CreateTableSchema(tableModel interface{}) bigquery.TableSchema { var schemas = &bigquery.TableSchema{} fields := []*bigquery.TableFieldSchema{} rt := reflect.TypeOf(tableModel) for i := 0; i < rt.NumField(); i++ { field := rt.Field(i) fields = append(fields, &bigquery.TableFieldSchema{ Mode: field.Tag.Get("bq_mode"), Name: field.Tag.Get("column"), Type: field.Tag.Get("bq_type"), }, ) } schemas.Fields = fields return *schemas } func CreateInsertRow(tableModel interface{}) []*bigquery.TableDataInsertAllRequestRows { rows := make([]*bigquery.TableDataInsertAllRequestRows, 0) row := &bigquery.TableDataInsertAllRequestRows{ Json: make(map[string]bigquery.JsonValue, 0), } rt := reflect.TypeOf(tableModel) rv := reflect.ValueOf(tableModel) for i := 0; i < rt.NumField(); i++ { value := rv.Field(i).Interface() field := rt.Field(i) row.Json[field.Tag.Get("column")] = value rows = append(rows, row) } return rows } func BqSample() { pemKeyBytes, err := ioutil.ReadFile(<your file path>) if err != nil { panic(err) } t, err := google.JWTConfigFromJSON(pemKeyBytes, "https://www.googleapis.com/auth/bigquery") if err != nil { panic(err) } /* Make DataSet */ client := t.Client(oauth2.NoContext) service, _ := bigquery.New(client) datasetList, _ := service.Datasets.List(projectId).All(false).Do() for _, dataset := range datasetList.Datasets { log.Print(dataset.DatasetReference.DatasetId) } /* DataSet Get */ Dataset, _ := service.Datasets.Get(projectId, datasetId).Do() if Dataset != nil { log.Print(Dataset.DatasetReference.DatasetId) } else { log.Print("dataset not exist") } ds := bigquery.Dataset{ DatasetReference:&bigquery.DatasetReference{ ProjectId:projectId, DatasetId:"test_dataset", }, } /* Table List */ service.Datasets.Insert(projectId, &ds).Do() TableList, _ := service.Tables.List(projectId, dataSetId).Do() for _, tableListTables := range TableList.Tables { log.Print(tableListTables.TableReference.TableId) } /* Table Get */ tableListTables, _ := service.Tables.Get( projectId, dataSetId, tableName).Do() if tableListTables != nil { log.Print(tableListTables.TableReference.TableId) } else { log.Print("table not exist") } /* Make Table */ schema := CreateTableSchema(TableModel{}) table := bigquery.Table{ TableReference: &bigquery.TableReference{ DatasetId: dataSetId, ProjectId: projectId, TableId: tableName, }, Schema:&schema, } tableRes, _ := service.Tables.Insert( projectId, dataSetId, &table).Do() log.Print(tableRes) /* InsertAll */ res, _ := service.Tabledata.InsertAll( projectId, dataSetId, tableName, &bigquery.TableDataInsertAllRequest{ Kind: "bigquery#tableDataInsertAllRequest", Rows: CreateInsertRow(TableModel{Name:"name", Address:"address"}), }).Do() log.Print(res) /* Query */ query := "SELECT address FROM [<project-name>:<dataset-name>.<table-name>] LIMIT 1000" result, err := service.Jobs.Query(projectId, &bigquery.QueryRequest{ Kind: "bigquery#queryRequest", Query: query, }).Do() for _, row := range result.Rows { log.Print(row) for _, cell := range row.F { log.Print(cell.V) } } }
gist