Skip to content

Commit

Permalink
add go resources
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Mar 8, 2023
1 parent 431f604 commit 75ca738
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
46 changes: 46 additions & 0 deletions cpg-language-go/src/test/resources/golang/declare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import "fmt"

func main() {
// Declaring multiple variables in a block with initializer. This is one
// GenDecl with two ValueSpec specs and one "Name" and (initializer) "Value"
// each.
//
// We translate this into one DeclarationStatement with two
// VariableDeclaration nodes
var (
a int = 1
b int = 2
)

// Declaring multiple variables in a single line. This is one GenDecl with
// one ValueSpec spec which contains two "Names" and two "Values" which
// correspond to the respective initializer values. Note, that the number of
// values MUST match the number of names
//
// We translate this into one DeclarationStatement with two
// VariableDeclaration nodes
var c, d = 3, 4

// Short assignment using an assignment, where all variables were not
// defined before. This is an AssignStmt which has DEFINE as its token.
//
// We need to split this up into several nodes. First, we translate this
// into one (implicit) DeclarationStatement with two VariableDeclaration
// nodes. Afterwards we are parsing it as a regular assignment.
e, f := 5, 6

// Short assignment using an assignment, where one variable (f) was defined
// before in the local scope. This is an AssignStmt which has DEFINE as its
// token. From the AST we cannot differentiate this from the previous
// example and we need to do a (local) variable lookup here.
//
// Finally, We need to split this up into several nodes. First, we translate
// this into one (implicit) DeclarationStatement with one
// VariableDeclaration node. Afterwards we are parsing it as a regular
// assignment.
f, g := 7, 8

fmt.Printf("%d %d %d %d %d %d %d\n", a, b, c, d, e, f, g)
}
21 changes: 21 additions & 0 deletions cpg-language-go/src/test/resources/golang/slices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "fmt"

func main() {
var a = []int{1,2,3}

// [1]
var b = a[:1]

// [2, 3]
var c = a[1:]

// [1]
var d = a[0:1]

// [1]
var e = a[0:1:1]

fmt.Printf("%v %v %v %v %v", a, b, c, d, e)
}
8 changes: 8 additions & 0 deletions cpg-language-go/src/test/resources/golang/type_constraints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

type MyStruct[T any] {}
type MyInterface{}

func main() {
_ := &MyStruct[MyInterface]{}
}

0 comments on commit 75ca738

Please sign in to comment.