Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: Go 2: support type generic programming #41847

Closed
higker opened this issue Oct 7, 2020 · 4 comments
Closed

proposal: Go 2: support type generic programming #41847

higker opened this issue Oct 7, 2020 · 4 comments

Comments

@higker
Copy link

@higker higker commented Oct 7, 2020

My version of Go are you using (go version)?

$ go version
go version go1.14 darwin/amd64

My proposals and needs:

Hope to support type generics in later versions to improve code writing efficiency.

  • This is an example of the current code
  • My Example Code:
package main

import (
	"fmt"
	"strconv"
)

type StackNode struct {
	Value interface{}
	Next  *StackNode
}

type Stack struct {
	Size     uint
	RootNode *StackNode
}

func NewStack() *Stack {
	return &Stack{Size: 0, RootNode: &StackNode{Value: 0, Next: nil}}
}

func (s *Stack) push(v interface{}) {
	if s.RootNode == nil { // 第一次
		s.RootNode.Next = &StackNode{Next: nil, Value: v}
		return
	}
	next := s.RootNode.Next
	s.RootNode.Next = &StackNode{Next: next, Value: v}
}

func (s *Stack) pop() interface{} {
	var node *StackNode = s.RootNode.Next
	if node == nil {
		return nil
	}
	s.RootNode.Next = node.Next
	return node.Value
}

func bracketMatching(v []rune, symbol []rune) bool {
	stack := NewStack()
	for _, r := range v {
		if r == symbol[0] {
			stack.push(symbol[0])
		}
		if r == symbol[1] {
			if stack.pop() == nil {
				return false
			}
		}
	}
	return stack.pop() == nil
}

func ReversePolishNotation(str []string) int64 {
	stack := NewStack()
	for _, s := range str {
		var result, o1, o2 int64
		switch s {
		case "+":
			o2, _ = strconv.ParseInt(stack.pop().(string), 10, 64)
			fmt.Println("O2", o2)
			o1, _ = strconv.ParseInt(stack.pop().(string), 10, 64)
			fmt.Println("O1", o1)
			result = o1 + o2
			stack.push(strconv.Itoa(int(result)))
		case "-":
			o2, _ = strconv.ParseInt(stack.pop().(string), 10, 64)
			fmt.Println("O2", o2)
			o1, _ = strconv.ParseInt(stack.pop().(string), 10, 64)
			fmt.Println("O1", o1)
			result = o1 - o2
			stack.push(strconv.Itoa(int(result)))
		case "*":
			o2, _ = strconv.ParseInt(stack.pop().(string), 10, 64)
			fmt.Println("O2", o2)
			o1, _ = strconv.ParseInt(stack.pop().(string), 10, 64)
			fmt.Println("O1", o1)
			result = o1 * o2
			stack.push(strconv.Itoa(int(result)))
		case "/":
			o2, _ = strconv.ParseInt(stack.pop().(string), 10, 64)
			fmt.Println("O2", o2)
			o1, _ = strconv.ParseInt(stack.pop().(string), 10, 64)
			fmt.Println("O1", o1)
			result = o1 / o2
			stack.push(strconv.Itoa(int(result)))
		default:
			stack.push(s)
		}

	}

	parseInt, err := strconv.ParseInt(stack.pop().(string), 10, 64)
	if err != nil {
		panic(err)
	}

	return parseInt
}

func main() {
	stack := NewStack()
	stack.push("0")
	stack.push("1")
	stack.push("2")
	stack.push("3")
	stack.push("4")
	stack.push(5)
	fmt.Println(stack.pop())
	fmt.Println(stack.pop())
	fmt.Println(stack.pop())
	fmt.Println(stack.pop())
	fmt.Println(stack.pop())
	fmt.Println(stack.pop())
	str := []string{"3", "17", "15", "-", "*", "18", "6", "/", "+"}
	fmt.Println(ReversePolishNotation(str))
}

The value type of the above StackNode struct can only be stored through interface{}, but I want to use generics to dynamically implement the type when it is created.

What did you expect to see?

  1. I look forward to supporting generics and simplifying code in Go2.
  2. My generic solution suggestion is that Go has a built-in T type, which replaces the scenarios where we sometimes need to use generics.
  • For example like this:
type StackNode<T> struct {
	Value T // Built-in T type 
	Next  *StackNode
}

// This is the dynamic creation type generic
func NewNode() *StackNode {
	return &StackNode{}<string>;
}
  • Thank you, the above is my idea and proposal
@gopherbot gopherbot added this to the Proposal milestone Oct 7, 2020
@gopherbot gopherbot added the Proposal label Oct 7, 2020
@seankhliao
Copy link
Contributor

@seankhliao seankhliao commented Oct 7, 2020

are you aware of the existence of the current generics draft? https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md

@OneOfOne
Copy link
Contributor

@OneOfOne OneOfOne commented Oct 7, 2020

@higker
Copy link
Author

@higker higker commented Oct 8, 2020

Oh, nice, thank you everyone, I hope golang has a bright future😁.

@ALTree
Copy link
Member

@ALTree ALTree commented Oct 8, 2020

Nice, let's merge this with the existing generics proposal then.

@ALTree ALTree closed this Oct 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.