'2017/09/05'에 해당되는 글 3건

  1. 2017.09.05 [golang] map 예제
  2. 2017.09.05 [golang] imported and not used
  3. 2017.09.05 [펌질] seq2sql



map을 생성할 때는 make를 사용한다. 


그리고 map의 값에 여러 매개 변수가 있을 때 구분자를 , 를 사용하는데, 끝에도 사용해야 에러가 발생하지 않는다. 


package main

import "fmt"

type Member struct {
name, dept string
a int
}

var m map[string]string
var members map[int]Member

func main() {
m = make(map[string]string)
m["key1"] = "value1"
m["key2"] = "value2"

fmt.Println(m["key1"])
fmt.Println(m["key2"])

members = make(map[int]Member)
members[1] = Member{
"samuel", "personal", 1,
}
members[2] = Member{
"jacks", "development", 2,
}

fmt.Println(members[1])
fmt.Println(members[2])
}

결과


value1

value2

{samuel personal 1}

{jacks development 2}





map 생성할 때 make를 사용하지 않고 일반 언어처럼 바로 생성할 수도 있다. 



var member2 = map[int]Member {
1 : {"hardy", "development", 1},
2 : {"jenny", "personal", 2},
}

fmt.Println(member2)


결과


map[1:{hardy development 1} 2:{jenny personal 2}]





2개를 리턴받을 수 있는데, 앞 매개변수는 값, 뒷 매개변수는 결과를 나타난다.


a, result := m["key2"]
fmt.Println(a, result)

b, result := members[2]
fmt.Println(b, result)

결과는 다음과 같다.


value2 true

{jacks development 2} true






map의 키,값, 키만, 값만 얻는 예제이다. 


fmt.Println("==1 (key value)")
for key, value := range members {
fmt.Println("Key:", key, "Value:", value)
}

fmt.Println("==2 (only key)")
for value := range members {
fmt.Println("value:", value)
}

fmt.Println("==3 (only value)")
for _, value := range members {
fmt.Println("value:", value)
}



결과는 다음과 같다. 


==1 (key value)

Key: 1 Value: {samuel personal 1}

Key: 2 Value: {jacks development 2}

==2 (only key)

value: 1

value: 2

==3 (only value)

value: {samuel personal 1}

value: {jacks development 2}





변경과 삭제는 다음과 같다.



m["key1"] = "newValue1"
fmt.Println(m["key1"])

delete(m, "key1")
fmt.Println(m["key1"])







Posted by '김용환'
,



https://golang.org/doc/faq#unused_variables_and_imports


Can I stop these complaints about my unused variable/import?

The presence of an unused variable may indicate a bug, while unused imports just slow down compilation, an effect that can become substantial as a program accumulates code and programmers over time. For these reasons, Go refuses to compile programs with unused variables or imports, trading short-term convenience for long-term build speed and program clarity.

Still, when developing code, it's common to create these situations temporarily and it can be annoying to have to edit them out before the program will compile.

Some have asked for a compiler option to turn those checks off or at least reduce them to warnings. Such an option has not been added, though, because compiler options should not affect the semantics of the language and because the Go compiler does not report warnings, only errors that prevent compilation.

There are two reasons for having no warnings. First, if it's worth complaining about, it's worth fixing in the code. (And if it's not worth fixing, it's not worth mentioning.) Second, having the compiler generate warnings encourages the implementation to warn about weak cases that can make compilation noisy, masking real errors that should be fixed.

It's easy to address the situation, though. Use the blank identifier to let unused things persist while you're developing.

import "unused"

// This declaration marks the import as used by referencing an
// item from the package.
var _ = unused.Item  // TODO: Delete before committing!

func main() {
    debugData := debug.Profile()
    _ = debugData // Used only during debugging.
    ....
} 
Nowadays, most Go programmers use a tool, goimports, which automatically rewrites a Go source file to have the correct imports, eliminating the unused imports issue in practice. This program is easily connected to most editors to run automatically when a Go source file is written.


 
import (
"fmt"
)


import "fmt"


./Test.go:3: imported and not used: "fmt"




아래와 같이 해야 에러가 발생하지 않는다. 



import (
_ "fmt"
)

func main() {

}






func main() {
i := 0
_ = i
}


Posted by '김용환'
,



By applying policy-based reinforcement learning with a query

execution environment to WikiSQL, our model Seq2SQL outperforms attentional

sequence to sequence models, improving execution accuracy from 35.9% to 60.3%

and logical form accuracy from 23.4% to 49.2%.



http://m.zdnet.co.kr/news_view.asp?article_id=20170831082744#imadnews

https://www.salesforce.com/blog/2017/08/salesforce-research-ai-talk-to-data

https://github.com/salesforce/WikiSQL

https://einstein.ai/static/images/layouts/research/seq2sql/seq2sql.pdf




Posted by '김용환'
,