Fix multi file handling in src packages
This commit is contained in:
23
_test/closure3.go
Normal file
23
_test/closure3.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
type T1 struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (t *T1) genAdd(k int) func(int) int {
|
||||
return func(i int) int {
|
||||
println(t.Name)
|
||||
return i + k
|
||||
}
|
||||
}
|
||||
|
||||
var t = &T1{"test"}
|
||||
|
||||
func main() {
|
||||
f := t.genAdd(4)
|
||||
println(f(5))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// test
|
||||
// 9
|
||||
7
_test/provider/foo.go
Normal file
7
_test/provider/foo.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package provider
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Foo() {
|
||||
fmt.Println("Hello from Foo")
|
||||
}
|
||||
@@ -10,6 +10,10 @@ func (t *T1) Info() {
|
||||
fmt.Println(t.Name)
|
||||
}
|
||||
|
||||
func Bar() {
|
||||
Foo()
|
||||
}
|
||||
|
||||
func Sample() {
|
||||
fmt.Println("Hello from Provider")
|
||||
}
|
||||
|
||||
32
_test/secure.gi
Normal file
32
_test/secure.gi
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/unrolled/secure" // or "gopkg.in/unrolled/secure.v1"
|
||||
)
|
||||
|
||||
var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("hello world"))
|
||||
})
|
||||
|
||||
func main() {
|
||||
secureMiddleware := secure.New(secure.Options{
|
||||
AllowedHosts: []string{"example.com", "ssl.example.com"},
|
||||
HostsProxyHeaders: []string{"X-Forwarded-Host"},
|
||||
SSLRedirect: true,
|
||||
SSLHost: "ssl.example.com",
|
||||
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
|
||||
STSSeconds: 315360000,
|
||||
STSIncludeSubdomains: true,
|
||||
STSPreload: true,
|
||||
FrameDeny: true,
|
||||
ContentTypeNosniff: true,
|
||||
BrowserXssFilter: true,
|
||||
ContentSecurityPolicy: "script-src $NONCE",
|
||||
PublicKey: `pin-sha256="base64+primary=="; pin-sha256="base64+backup=="; max-age=5184000; includeSubdomains; report-uri="https://www.example.com/hpkp-report"`,
|
||||
})
|
||||
|
||||
app := secureMiddleware.Handler(myHandler)
|
||||
http.ListenAndServe("127.0.0.1:3000", app)
|
||||
}
|
||||
@@ -13,8 +13,8 @@ type Middleware struct {
|
||||
}
|
||||
|
||||
func (m *Middleware) Handler(w http.ResponseWriter, r *http.Request) {
|
||||
println("Hello")
|
||||
log.Println(r.Header.Get("User-Agent"))
|
||||
//println("Hello")
|
||||
//log.Println(r.Header.Get("User-Agent"))
|
||||
log.Println(w.Header())
|
||||
fmt.Fprintln(w, "Welcome to my website", m.Name)
|
||||
}
|
||||
|
||||
@@ -4,12 +4,10 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello world")) })
|
||||
|
||||
//var myHandler = func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello world")) }
|
||||
//func myHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello world")) }
|
||||
var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("hello world"))
|
||||
})
|
||||
|
||||
func main() {
|
||||
//http.HandleFunc("/", myHandler)
|
||||
http.ListenAndServe(":8080", myHandler)
|
||||
}
|
||||
|
||||
10
_test/src3.go
Normal file
10
_test/src3.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import "github.com/containous/gi/_test/provider"
|
||||
|
||||
func main() {
|
||||
provider.Bar()
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Hello from Foo
|
||||
16
_test/time6.gi
Normal file
16
_test/time6.gi
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t := &time.Time{}
|
||||
t.UnmarshalText([]byte("1985-04-12T23:20:50.52Z"))
|
||||
|
||||
fmt.Println(t)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 1985-04-12 23:20:50.52 +0000 UTC
|
||||
11
_test/type4.go
Normal file
11
_test/type4.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a := int32(12)
|
||||
fmt.Println(reflect.TypeOf(a))
|
||||
}
|
||||
@@ -60,6 +60,7 @@ func (interp *Interpreter) Cfg(root *Node, sdef *NodeMap) []*Node {
|
||||
var exports *SymMap
|
||||
var expval *ValueMap
|
||||
var iotaValue int
|
||||
var srcPkg *NodeMap
|
||||
|
||||
// Fill root scope with initial symbol definitions
|
||||
for name, node := range *sdef {
|
||||
@@ -84,6 +85,10 @@ func (interp *Interpreter) Cfg(root *Node, sdef *NodeMap) []*Node {
|
||||
|
||||
case File:
|
||||
pkgName := n.child[0].ident
|
||||
srcPkg = interp.srcPkg[pkgName]
|
||||
if srcPkg == nil {
|
||||
srcPkg = &NodeMap{}
|
||||
}
|
||||
if pkg, ok := interp.Exports[pkgName]; ok {
|
||||
exports = pkg
|
||||
expval = interp.Expval[pkgName]
|
||||
@@ -617,6 +622,11 @@ func (interp *Interpreter) Cfg(root *Node, sdef *NodeMap) []*Node {
|
||||
}
|
||||
}
|
||||
n.recv = n
|
||||
} else if node, ok := (*srcPkg)[n.ident]; ok {
|
||||
n.val = node
|
||||
n.typ = node.typ
|
||||
n.kind = node.kind
|
||||
n.findex = node.findex
|
||||
} else {
|
||||
frameIndex.max++
|
||||
scope.sym[n.ident] = &Symbol{index: frameIndex.max}
|
||||
|
||||
@@ -455,6 +455,37 @@ func main() {
|
||||
|
||||
}
|
||||
|
||||
func Example_closure3() {
|
||||
src := `
|
||||
package main
|
||||
|
||||
type T1 struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (t *T1) genAdd(k int) func(int) int {
|
||||
return func(i int) int {
|
||||
println(t.Name)
|
||||
return i + k
|
||||
}
|
||||
}
|
||||
|
||||
var t = &T1{"test"}
|
||||
|
||||
func main() {
|
||||
f := t.genAdd(4)
|
||||
println(f(5))
|
||||
}
|
||||
`
|
||||
i := NewInterpreter(Opt{Entry: "main"})
|
||||
i.ImportBin(export.Pkg)
|
||||
i.Eval(src)
|
||||
|
||||
// Output:
|
||||
// test
|
||||
// 9
|
||||
}
|
||||
|
||||
func Example_const0() {
|
||||
src := `
|
||||
package main
|
||||
@@ -2279,8 +2310,8 @@ type Middleware struct {
|
||||
}
|
||||
|
||||
func (m *Middleware) Handler(w http.ResponseWriter, r *http.Request) {
|
||||
println("Hello")
|
||||
log.Println(r.Header.Get("User-Agent"))
|
||||
//println("Hello")
|
||||
//log.Println(r.Header.Get("User-Agent"))
|
||||
log.Println(w.Header())
|
||||
fmt.Fprintln(w, "Welcome to my website", m.Name)
|
||||
}
|
||||
@@ -2331,9 +2362,9 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
//func myHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello world")) }
|
||||
//var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello world")) })
|
||||
//var myHandler = func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello world")) }
|
||||
func myHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello world")) }
|
||||
var myHandler = func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello world")) }
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", myHandler)
|
||||
@@ -2366,6 +2397,63 @@ func main() {
|
||||
|
||||
}
|
||||
|
||||
func Example_server5() {
|
||||
src := `
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("hello world"))
|
||||
})
|
||||
|
||||
func main() {
|
||||
http.ListenAndServe(":8080", myHandler)
|
||||
}`
|
||||
i := NewInterpreter(Opt{Entry: "main"})
|
||||
i.ImportBin(export.Pkg)
|
||||
i.Eval(src)
|
||||
|
||||
}
|
||||
|
||||
func Example_server6() {
|
||||
src := `
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("hello world"))
|
||||
})
|
||||
|
||||
type T1 struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (t *T1) Handler(h http.Handler) http.Handler {
|
||||
fmt.Println("#1", t.Name)
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("#2", t.Name)
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := &T1{"myName"}
|
||||
handler := t.Handler(myHandler)
|
||||
http.ListenAndServe(":8080", handler)
|
||||
}`
|
||||
i := NewInterpreter(Opt{Entry: "main"})
|
||||
i.ImportBin(export.Pkg)
|
||||
i.Eval(src)
|
||||
|
||||
}
|
||||
|
||||
func Example_sieve() {
|
||||
src := `
|
||||
// A concurrent prime sieve
|
||||
@@ -2488,6 +2576,24 @@ func main() {
|
||||
// myName
|
||||
}
|
||||
|
||||
func Example_src3() {
|
||||
src := `
|
||||
package main
|
||||
|
||||
import "github.com/containous/gi/_test/provider"
|
||||
|
||||
func main() {
|
||||
provider.Bar()
|
||||
}
|
||||
`
|
||||
i := NewInterpreter(Opt{Entry: "main"})
|
||||
i.ImportBin(export.Pkg)
|
||||
i.Eval(src)
|
||||
|
||||
// Output:
|
||||
// Hello from Foo
|
||||
}
|
||||
|
||||
func Example_str() {
|
||||
src := `
|
||||
package main
|
||||
|
||||
Reference in New Issue
Block a user